Django:在创建 post 时未找到 post 匹配查询
Django: No post found matching the query when creating a post
我刚刚使用 django-autoslug 为我的 Post 模型工作了 slug。但是当我尝试创建一个新的 post:
时,我现在遇到了这个错误
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/post/new/
Raised by: blog.views.PostDetailView
No post found matching the query
Post 型号
class Post(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(User, on_delete=models.CASCADE)
slug = AutoSlugField(populate_from='title', null=True)
def save(self, *args, **kwargs):
self.slug = self.slug or slugify(self.title)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('post-detail', kwargs={'slug': self.slug, 'author': self.author})
views.py
class PostDetailView(DetailView):
model = Post
def get_queryset(self, *args, **kwargs):
return super().get_queryset(*args, **kwargs).filter(author__username=self.kwargs['author'])
urls.py
urlpatterns = [
path('', views.home, name='blog-home'),
path('<str:author>/<slug:slug>/', PostDetailView.as_view(), name='post-detail')
path('post/new/', PostCreateView.as_view(), name='post-create'),
path('<str:author>/<slug:slug>/update/', PostUpdateView.as_view(), name='post-update'),
path('<str:author>/<slug:slug>/delete/', PostDeleteView.as_view(), name='post-delete'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
如果您请求 /post/new
URL,那么这将匹配 post-detail
视图,因为它可以将 post
与 author
匹配,并且new
与 slug
.
您应该重新排序 url 模式,使其首先匹配 post-create
视图:
urlpatterns = [
path('', views.home, name='blog-home'),
<b>path('post/new/', PostCreateView.as_view(), name='post-create')</b>,
path('<str:author>/<slug:slug>/', PostDetailView.as_view(), name='post-detail')
path('<str:author>/<slug:slug>/update/', PostUpdateView.as_view(), name='post-update'),
path('<str:author>/<slug:slug>/delete/', PostDeleteView.as_view(), name='post-delete'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
首先指定post-create
视图,如果用户访问/post/new/
,就会触发post-create
视图。
这也意味着作者post
不能写一篇名为new
的文章,这样就不会触发post-detail
视图。
我有点作弊,但我放弃了 slug,我的网站转到 tachlis.herokuapp。com/item_id/title_slug(例如 http://tachlis.herokuapp.com/552/curb-your-veganism),然后是我的 urls.py 是 'int:pk/str:pkv/',我使用项目 ID ('pk') 来获取正确的项目,而不是 slug。
我刚刚使用 django-autoslug 为我的 Post 模型工作了 slug。但是当我尝试创建一个新的 post:
时,我现在遇到了这个错误Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/post/new/
Raised by: blog.views.PostDetailView
No post found matching the query
Post 型号
class Post(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(User, on_delete=models.CASCADE)
slug = AutoSlugField(populate_from='title', null=True)
def save(self, *args, **kwargs):
self.slug = self.slug or slugify(self.title)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('post-detail', kwargs={'slug': self.slug, 'author': self.author})
views.py
class PostDetailView(DetailView):
model = Post
def get_queryset(self, *args, **kwargs):
return super().get_queryset(*args, **kwargs).filter(author__username=self.kwargs['author'])
urls.py
urlpatterns = [
path('', views.home, name='blog-home'),
path('<str:author>/<slug:slug>/', PostDetailView.as_view(), name='post-detail')
path('post/new/', PostCreateView.as_view(), name='post-create'),
path('<str:author>/<slug:slug>/update/', PostUpdateView.as_view(), name='post-update'),
path('<str:author>/<slug:slug>/delete/', PostDeleteView.as_view(), name='post-delete'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
如果您请求 /post/new
URL,那么这将匹配 post-detail
视图,因为它可以将 post
与 author
匹配,并且new
与 slug
.
您应该重新排序 url 模式,使其首先匹配 post-create
视图:
urlpatterns = [
path('', views.home, name='blog-home'),
<b>path('post/new/', PostCreateView.as_view(), name='post-create')</b>,
path('<str:author>/<slug:slug>/', PostDetailView.as_view(), name='post-detail')
path('<str:author>/<slug:slug>/update/', PostUpdateView.as_view(), name='post-update'),
path('<str:author>/<slug:slug>/delete/', PostDeleteView.as_view(), name='post-delete'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
首先指定post-create
视图,如果用户访问/post/new/
,就会触发post-create
视图。
这也意味着作者post
不能写一篇名为new
的文章,这样就不会触发post-detail
视图。
我有点作弊,但我放弃了 slug,我的网站转到 tachlis.herokuapp。com/item_id/title_slug(例如 http://tachlis.herokuapp.com/552/curb-your-veganism),然后是我的 urls.py 是 'int:pk/str:pkv/',我使用项目 ID ('pk') 来获取正确的项目,而不是 slug。