from django.shortcuts import render, get_object_or_404, redirect
from . models import *
class ObjectDetailMixin:
model = None
template = None
def get(self, request, slug):
obj = get_object_or_404(self.model, slug__iexact=slug)
return render(request, self.template, context={self.model.__name__.lower(): obj})
class ObjectCreateMixin:
model_form = None
template = None
def get(self,request):
form = self.model_form()
return render(request, self.template, context={'form': form})
def post(self, request):
bound_form = self.model_form(request.POST)
if bound_form.is_valid():
new_obj = bound_form.save()
return redirect(new_obj)
return render(request, self.template, context={'form': bound_form})
class ObjectUpdateMixin:
model = None
model_form = None
template = None
def get(self, request, slug):
obj = self.model.objects.get(slug__iexact=slug)
bound_form = self.model_form(instance=obj)
return render(request, self.template, context={'form': bound_form, self.model.__name__.lower(): obj})
def post(self, request, slug):
obj = self.model.objects.get(slug__iexact=slug)
bound_form = self.model_form(request.POST, instance=obj)
if bound_form.is_valid():
new_obj = bound_form.save()
return redirect(new_obj)
return render(request, self.template, context={'form': bound_form, self.model.__name__.lower(): obj})
from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic import View
from . models import Post, Tag
from . utils import *
from . forms import PostForm, TagForm
def index(request):
return render(request, 'main/index.html')
def post(request):
posts = Post.objects.all()
return render(request, 'blog/index.html', context={'posts': posts})
def tag(request):
tags = Tag.objects.all()
return render(request, 'blog/tags.html', context={'tags': tags})
class PostInfo(ObjectDetailMixin, View):
model = Post
template = 'blog/post.html'
class PostCreate(ObjectCreateMixin, View):
model_form = PostForm
template = 'blog/post_create.html'
class PostUpdate(ObjectCreateMixin, View):
model = Post
model_form = PostForm
template = 'blog/post_update.html'
class TagInfo(ObjectDetailMixin, View):
model = Tag
template = 'blog/tag_info.html'
class TagCreate(ObjectCreateMixin, View):
model_form = TagForm
template = 'blog/tag_create.html'
class TagUpdate(ObjectUpdateMixin, View):
model = Tag
model_form = TagForm
template = 'blog/tag_update.html'
# def get(self, request, slug):
# tag = Tag.objects.get(slug__iexact=slug)
# bound_form = TagForm(instance=tag)
# return render(request, 'blog/tag_update.html', context={'form': bound_form, 'tag': tag})
# def post(self, request, slug):
# tag = Tag.objects.get(slug__iexact=slug)
# bound_form = TagForm(request.POST, instance=tag)
# if bound_form.is_valid():
# new_tag = bound_form.save()
# return redirect(new_tag)
# return render(request, 'blog/tag_update.html', context={'form': bound_form, 'tag': tag})
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/posts/new-post-1555763604/update/
Django Version: 2.1.5
Python Version: 3.7.2
Installed Applications:
['blog',
'engine',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
Exception Type: TypeError at /posts/new-post-1555763604/update/
Exception Value: get() got an unexpected keyword argument 'slug'
from django.db import models
from django.shortcuts import reverse
from django.utils.text import slugify
from time import time
def gen_slug(s):
new_slug = slugify(s, allow_unicode=True)
return new_slug + '-' + str(int(time()))
class Post(models.Model):
title = models.CharField(max_length=150, db_index=True)
slug = models.SlugField(max_length=150, blank=True, unique=True)
body = models.TextField(blank=True, db_index=True)
tags = models.ManyToManyField('Tag', blank=True, related_name='posts')
date_pub = models.DateTimeField(blank=True, null=True)
def get_absolute_url(self):
return reverse('post_url', kwargs={'slug': self.slug})
def get_update_url(self):
return reverse('post_update_url', kwargs={'slug': self.slug})
def save(self, *args, **kwargs):
if not self.id:
self.slug = gen_slug(self.title)
super().save(*args, **kwargs)
def __str__(self):
return self.title
class Tag(models.Model):
title = models.CharField(max_length=50)
slug = models.SlugField(max_length=50, unique=True)
def get_absolute_url(self):
return reverse('tag_url', kwargs={'slug': self.slug})
def get_update_url(self):
return reverse('tag_update_url', kwargs={'slug': self.slug})
def __str__(self):
return self.title
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from . models import *
class ObjectDetailMixin:
model = None
template = None
def get(self, request, slug):
obj = get_object_or_404(self.model, slug__iexact=slug)
return render(request, self.template, context={self.model.__name__.lower(): obj})
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from . models import *
class ObjectDetailMixin:
model = None
template = None
def get(self, request, slug):
obj = get_object_or_404(self.model, slug__iexact=slug)
return render(request, self.template, context={self.model.__name__.lower(): obj})
DoesNotExist at /tags/dj
Post matching query does not exist.
Request Method: GET
Request URL: http://127.0.0.1:8000/tags/dj
Django Version: 2.1.5
Exception Type: DoesNotExist
Exception Value:
Post matching query does not exist.
Exception Location: /usr/local/lib/python3.7/site-packages/django/db/models/query.py in get, line 399
Python Executable: /usr/local/bin/python3.7
Python Version: 3.7.2
Python Path:
['/home/bred/codes/my_sites/Blog',
'/usr/local/lib/python37.zip',
'/usr/local/lib/python3.7',
'/usr/local/lib/python3.7/lib-dynload',
'/usr/local/lib/python3.7/site-packages']
Server time: Sun, 7 Apr 2019 16:16:02 +0000