在views.py ### category_posts = post.objects.filter(category = cat)###返回一个空查询集
我正在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过我了解您要执行的操作,您需要以下内容:
使用
cat
作为参数,因为您在URL中使用了&lt; str:cat&gt;
。 py,否则您可能会遇到“意外参数”错误。您必须通过
类别
过滤,而不是作者
。您的问题是正确的,但您的代码不是。您无需在上下文中通过Cat Varibale,因为任何帖子都会包含它。
服用@willem van onSem和@roham建议:
如果要使用slugs,也许是结帐
models.slugfield
。By what I understand you're trying to do you want the following:
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
, notauthor
. 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:
If you want to use slugs, maybe checkout
models.SlugField
.