Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据类别筛选帖子_Python_Django_Python 3.x_Filter - Fatal编程技术网

Python 根据类别筛选帖子

Python 根据类别筛选帖子,python,django,python-3.x,filter,Python,Django,Python 3.x,Filter,我是django的新手,通常是编程方面的。现在我写了我的第一个项目——那将是我的个人博客,除了一个功能外,几乎完成了: 我在页面右侧的面板中显示了类别列表 1问题是如何按这些类别对我的帖子进行排序/筛选?我的意思是,我想点击右面板中的一个类别,查看提到该类别的帖子,帖子可能有几个类别 我尝试了很多组合,在stackoverflow上可以找到,但是我以前没有这样做,所以我不知道如何在我的项目中实现这个特性 models.py class Category(models.Model): ti

我是django的新手,通常是编程方面的。现在我写了我的第一个项目——那将是我的个人博客,除了一个功能外,几乎完成了: 我在页面右侧的面板中显示了类别列表

1问题是如何按这些类别对我的帖子进行排序/筛选?我的意思是,我想点击右面板中的一个类别,查看提到该类别的帖子,帖子可能有几个类别

我尝试了很多组合,在stackoverflow上可以找到,但是我以前没有这样做,所以我不知道如何在我的项目中实现这个特性

models.py

class Category(models.Model):
    title = models.CharField(max_length=20)

    def __str__(self):
        return self.title


class Post(models.Model):
    title = models.CharField(max_length=100)
    overview = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)
    content = HTMLField('Content')
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    thumbnail = models.ImageField()
    categories = models.ManyToManyField(Category)
    featured = models.BooleanField()
    previous_post = models.ForeignKey('self', related_name='previous', on_delete=models.SET_NULL, blank=True, null=True)
    next_post = models.ForeignKey('self', related_name='next', on_delete=models.SET_NULL, blank=True, null=True)

    def __str__(self):
        return self.title
views.py

def filter_by_category(request):
    post_list = Post.objects.filter(categories=)
    context = {
        'post_list': post_list
    }
    return render(request, 'post_by_category.html', context)
url.py

urlpatterns = [
   path('admin/', admin.site.urls),
   path('', index),
   path('blog/', blog, name='post-list'),
   path('search/', search, name='search'),
   path('blog/filter_by_category/', filter_by_category, name='filter_by_category'),
   path('subscribe/', email_list_signup, name='subscribe'),
   path('create/', post_create, name='post-create'),
   path('post/<id>/', post, name='post-detail'),
   path('post/<id>/update/', post_update, name='post-update'),
   path('post/<id>/delete/', post_delete, name='post-delete'),
   path('tinymce/', include('tinymce.urls')),
   path('accounts/', include('allauth.urls')),
sidebar.html

<div class="widget categories">
    <header>
      <h3 class="h6">Categories</h3>
    </header>
    {% for cat in category_count %}
    <div class="item d-flex justify-content-between">
      <a href="{% url 'filter_by_category' %}">{{ cat.categories__title }}</a><span>{{ cat.categories__title__count }}</span></div>
    {% endfor %}
  </div>
给定的代码不起作用,我知道views.py中的问题。我完全搞不懂怎么正确地写。 请帮我解决这个问题

更新: 问题解决了,多亏了兰德克罗斯。
我曾一度接近那个决定,但却搞砸了。谢谢大家!

首先,展开url定义,如下所示:

path('blog/filter_by_category/<str:category>', filter_by_category, name='filter_by_category'),
并更改模板,使其将参数添加到url:

<div class="widget categories">
    <header>
      <h3 class="h6">Categories</h3>
    </header>
    {% for cat in category_count %}
    <div class="item d-flex justify-content-between">
      <a href="{% url 'filter_by_category' category=cat.categories__title %}">{{ cat.categories__title }}</a><span>{{ cat.categories__title__count }}</span></div>
    {% endfor %}
  </div>

您还可以将url更改为int,并将模板更改为使用类别ID而不是标题(如果您更喜欢或更适合您),例如类别标题可以包含重复项。

这很好。我是否可以修改,使该类别的名称在Url中为小写字母?我的URL像/posts/mathematics/这样构建。。我希望类别是小写的。@Julianto您的url是大写还是小写不是由url/路径定义定义的,而是由生成url的任何内容定义的。如果您像上面那样在模板中“手动”构建url,您可以使用较低的模板标记使文本全部小写:但是,我也建议您查看django.utils.text中的django SlugField和/或slagify方法。
<div class="widget categories">
    <header>
      <h3 class="h6">Categories</h3>
    </header>
    {% for cat in category_count %}
    <div class="item d-flex justify-content-between">
      <a href="{% url 'filter_by_category' category=cat.categories__title %}">{{ cat.categories__title }}</a><span>{{ cat.categories__title__count }}</span></div>
    {% endfor %}
  </div>