Python 无法将博客文章链接到特定类别Django

Python 无法将博客文章链接到特定类别Django,python,django,django-models,django-views,django-urls,Python,Django,Django Models,Django Views,Django Urls,我使用Tango和Django创建了一个包含类别的数据库,并使用Django女孩教程向数据库中添加了一个博客。两者都很好,但我一直很难将每个博客文章链接到各自的类别。现在,所有帖子都转到我的post_list.html页面 如果我从头开始做这件事,我会添加一个新视图,添加一个新模板,添加一个url映射,然后从category页面添加一个链接。我也知道我需要编辑我的博客文章模型,以包含: category - models.ForeignKey(Category) 我想这很简单,只需将以下内容添

我使用Tango和Django创建了一个包含类别的数据库,并使用Django女孩教程向数据库中添加了一个博客。两者都很好,但我一直很难将每个博客文章链接到各自的类别。现在,所有帖子都转到我的post_list.html页面

如果我从头开始做这件事,我会添加一个新视图,添加一个新模板,添加一个url映射,然后从category页面添加一个链接。我也知道我需要编辑我的博客文章模型,以包含:

category - models.ForeignKey(Category)
我想这很简单,只需将以下内容添加到我的url.py:

url(r'^category/(?P<category_name_slug>[\w\-]+)/post_edit/$', views.add_page, name='add_page'),  
我的URL.py

    url(r'^category/(?P<category_name_slug>[\w\-]+)/$', views.category, name='category'),
    url(r'^post_list/$', views.post_list, name='post_list'),
    url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
    url(r'^post/new/$', views.post_new, name='post_new'),
    url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name='post_edit'),

提前感谢。

我意识到我必须将我的帖子过滤到它们各自的类别。我还转而使用基于类的视图。代码比较干净

class PostListView(DetailView):
model = Category
template_name = 'dicer/post_list.html'

def get_context_data(self, **kwargs):
    context = super(PostListView, self).get_context_data(**kwargs)
    posts = (
        Post.objects.filter(category=self.get_object(),
                            published_date__lte=timezone.now())
                    .order_by('published_date')
    )
    context['posts'] = posts
    return context

你有什么问题?到目前为止你都试了些什么?@GwynBleidD也这么说。你在描述你的整个情况,你说你有很多错误,所以让我们从一个特定的错误开始。第一个错误-尝试进行迁移:你试图添加一个不可为空的字段“category”,以在没有默认值的情况下发布;我们不能这样做(数据库需要一些东西来填充现有的行)。请选择修复1)立即提供一次性默认值(将在所有现有行上设置)2)退出,并让我在models.py I added category=models.ForeignKey(category)中添加默认值以发布模型,并更新发布编辑视图,以def post_edit(请求、pk、category_name_slug)和url(r'^category/(\P[\w-]+)/post_edit/$,views.add_page,name='post_edit')好的,这是一个好的开始。对于可为空的字段,您需要:
category=models.ForeignKey(category,null=True)
。还有什么?除了迁移它,不需要做其他事情。必须将模型导入post_new views.py。我将其编辑为:def post_new(request,category_name_slug):对于url,post_new应该类似于:url(r'^category/(?P[\w \-]+)/post_new/$”,views.post_new,name='post_new'),这给了我以下错误:找不到/rango/category/rango/Reverse中带有参数的'post_new和关键字参数'{}'。尝试了1个模式:['rango/category/(?P[\\w\\-]+)/post\u new/$”]我不太清楚这是什么意思
 def post_list(request):
 posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
 return render(request, 'rango/post_list.html', {'posts': posts})

def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'rango/post_detail.html', {'post': post})

def post_new(request):
if request.method == "POST":
    form = PostForm(request.POST)
    if form.is_valid():
        post = form.save(commit=False)
        post.author = request.user
        post.published_date = timezone.now()
        post.save()
        return redirect('post_detail', pk=post.pk)
else:
    form = PostForm()
return render(request, 'rango/post_edit.html', {'form': form})

def post_edit(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
    form = PostForm(request.POST, instance=post)
    if form.is_valid():
        post = form.save(commit=False)
        post.author = request.user
        post.published_date = timezone.now()
        post.save()
        return redirect('post_detail', pk=post.pk)
else:
    form = PostForm(instance=post)
return render(request, 'rango/post_edit.html', {'form': form})
class PostListView(DetailView):
model = Category
template_name = 'dicer/post_list.html'

def get_context_data(self, **kwargs):
    context = super(PostListView, self).get_context_data(**kwargs)
    posts = (
        Post.objects.filter(category=self.get_object(),
                            published_date__lte=timezone.now())
                    .order_by('published_date')
    )
    context['posts'] = posts
    return context