在views.py file ==> category_posts = post.object.filter(category = cat)返回一个空查询集。 POST'模型有类别的示例帖子

发布于 2025-01-25 22:41:22 字数 2037 浏览 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,cat):
    category_posts = Post.objects.filter(category=cat)
    return render(request,'categories.html',{'category_posts':category_posts})

urls.py

urls.py文件。

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

categories.html

这将是最终显示页面,其中category_posts显示为一个空的querySet。 FO循环无法运行,因为category_posts是一个空的QuerySet。单个单词类别也为空(排除一个slugify问题)

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

<ul>
{% for post 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>
     <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. I have spent two full days trying to figure this out and still
got nothing.

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,cat):
    category_posts = Post.objects.filter(category=cat)
    return render(request,'categories.html',{'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. Single word categories are also empty(to rule out a slugify issue)

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

<ul>
{% for post 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>
     <div class="overlay">
    </a>
</li>
{% endfor %}

{% endblock %}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文