在views.py ### category_posts = post.objects.filter(category = cat)###返回一个空查询集

发布于 2025-01-26 05:12:17 字数 1979 浏览 3 评论 0原文

我正在Django创建一个博客应用程序,在那里我遇到了这个不寻常的问题。我无法过滤和显示博客文章类别。请在下面找到我的代码。提前致谢。

py

这是我为博客文章创建的模型。

class Post(models.Model):
    title = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    body = models.TextField()
    post_date = models.DateField(auto_now_add=True)
    category = models.CharField(max_length=255, default="uncategorized")

    def __str__(self):
        return self.title + ' | ' + str(self.author)

    def get_absolute_url(self):
        #return reverse('article-detail', args=(str(self.id)))
        return reverse('homepage')

views.py

这是我为明智的博客文章创建的观点。

def CategoryView(request,cats):
    category_posts = Post.objects.filter(author= 'apaar')
    return render(request,'categories.html',{'cat':cat,'category_posts':category_posts})

urls.py

urls.py文件。

    path('category/<str:cat>/', CategoryView, name = 'category'),

categories.html

这将是最终显示页面,其中category_posts显示为一个空的querySet。 FO循环无法运行,因为category_posts是一个空的QuerySet。

{% extends 'base.html' %}
{% load static %}

{% block content %}
<h1>Categories</h1>
<ul>
{% for posts in category_posts %}
<li>
    <div class="category"><a href="{% url 'category' post.category|slugify %}">{{ post.category }}</a></div>
    <a href="{% url 'article-detail' post.pk  %}"><h3><b>{{ post.title }} </b></h3>

    <p class=card-date>    <big>{{ post.author }}</big><small> -{{ post.post_date }}     </small> </p>
    <p class="excerpt-text">{{ post.body | slice:"100" |safe}}</p>

    <div class="article-thumbnail">
      <img src="{% static "images/bg.png" %}" alt="article-thumbnail" >
    <div class="overlay"></a>
</li>
{% endfor %}

{% endblock %}

I am creating a blog application in django where i encountered this unusual issue. I am not being able to filter and display blog posts category wise. Please find my code below. Thanks in advance.

MODELS.py

This is the model which i have created for a blog post.

class Post(models.Model):
    title = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    body = models.TextField()
    post_date = models.DateField(auto_now_add=True)
    category = models.CharField(max_length=255, default="uncategorized")

    def __str__(self):
        return self.title + ' | ' + str(self.author)

    def get_absolute_url(self):
        #return reverse('article-detail', args=(str(self.id)))
        return reverse('homepage')

VIEWS.py

This is the view that i have created for a category wise blog posts.

def CategoryView(request,cats):
    category_posts = Post.objects.filter(author= 'apaar')
    return render(request,'categories.html',{'cat':cat,'category_posts':category_posts})

URLS.py

The urls.py file.

    path('category/<str:cat>/', CategoryView, name = 'category'),

CATEGORIES.html

This will be the final display page where the category_posts is displayed as an empty queryset. The for loop is unable to run because category_posts is an empty queryset.

{% extends 'base.html' %}
{% load static %}

{% block content %}
<h1>Categories</h1>
<ul>
{% for posts in category_posts %}
<li>
    <div class="category"><a href="{% url 'category' post.category|slugify %}">{{ post.category }}</a></div>
    <a href="{% url 'article-detail' post.pk  %}"><h3><b>{{ post.title }} </b></h3>

    <p class=card-date>    <big>{{ post.author }}</big><small> -{{ post.post_date }}     </small> </p>
    <p class="excerpt-text">{{ post.body | slice:"100" |safe}}</p>

    <div class="article-thumbnail">
      <img src="{% static "images/bg.png" %}" alt="article-thumbnail" >
    <div class="overlay"></a>
</li>
{% endfor %}

{% endblock %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日情怀 2025-02-02 05:12:17

通过我了解您要执行的操作,您需要以下内容:

def CategoryView(request,cat):
    category_posts = Post.objects.filter(category=cat)
    return render(request,'categories.html',{'category_posts':category_posts})

使用cat作为参数,因为您在URL中使用了&lt; str:cat&gt;。 py,否则您可能会遇到“意外参数”错误。
您必须通过类别过滤,而不是作者。您的问题是正确的,但您的代码不是。
您无需在上下文中通过Cat Varibale,因为任何帖子都会包含它。

服用@willem van onSem和@roham建议:

{% for post in category_posts %}
# ...
    <div class="category"><a href="{% url 'category' post.category    %}">{{ post.category }}</a></div>

如果要使用slugs,也许是结帐models.slugfield

By what I understand you're trying to do you want the following:

def CategoryView(request,cat):
    category_posts = Post.objects.filter(category=cat)
    return render(request,'categories.html',{'category_posts':category_posts})

Use cat as your parameter, since you're using <str:cat> in your urls.py, otherwise you might get an "Unexpected parameter" error.
You have to filter by category, not author. Youre question is correct, but your code isn't.
You don't need to pass cat varibale in context, since any posts would contain it within themselves.

Taking @Willem Van Onsem and @Roham suggestions:

{% for post in category_posts %}
# ...
    <div class="category"><a href="{% url 'category' post.category    %}">{{ post.category }}</a></div>

If you want to use slugs, maybe checkout models.SlugField.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文