Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.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 如何按博客中的每个类别检索文章,并将它们放在django中的postlist html页面上_Python_Html_Django - Fatal编程技术网

Python 如何按博客中的每个类别检索文章,并将它们放在django中的postlist html页面上

Python 如何按博客中的每个类别检索文章,并将它们放在django中的postlist html页面上,python,html,django,Python,Html,Django,我在django创建了一个博客和一个新闻网站,在它的主页上显示了一个特定的类别。 这是我的post_list.html上显示的新闻、政治和教育的特定类别。 当我试图通过 Post.objects.filter(category\uuuu name\uuuu in=[“news”]) 这是我的类别的模型 class Category(models.Model): name = models.CharField(max_length=250) slug = mo

我在django创建了一个博客和一个新闻网站,在它的主页上显示了一个特定的类别。 这是我的post_list.html上显示的新闻、政治和教育的特定类别。 当我试图通过

Post.objects.filter(category\uuuu name\uuuu in=[“news”])

这是我的类别的模型

class Category(models.Model): 
         name = models.CharField(max_length=250)
         slug = models.SlugField(max_length=250, unique=True)
这是我贴的模型

class Post(models.Model):
    image = models.ImageField(upload_to="images/")
    title = models.CharField(max_length=150)
    summary = models.CharField(max_length=250)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
*这是我的帖子列表的视图

class PostListView(ListView):
       model = Post 
       template_name = blog/post_list.html
       context_object_name = "posts"
       ordering = ["date_posted"
       paginate_by = 5


def get_queryset(self):
    posts = 
    get_object_or_404(Post)
    return  Post.objects.filter().order_by( "-date_posted")


def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['posts'] = Post.objects.filter()[0:4] 
    context['new_cat'] =Post.objects.filter(category__name__in=[" news"])[0.5] 
    context['new_politics'] = Post.objects.filter(category__name__in=["politics"])[0.5]
 
    return context ***
这是我的pos_tlist.html代码,用于在新闻中按类别检索帖子

新闻
{新目录中帖子的百分比为%}
div class=“mt-2”
'''
当我运行python manage.py runserver时,我得到了一个为这篇文章提供的无例外 但是当我转到我的shell时,python manage.py shell 我得到了所有帖子的完整列表。 但它拒绝在hompage上显示 我需要帮助做这件事,并使我所有的类别出现在每个类别的新闻页面 有没有办法让我所有的帖子在post_list.html和主页上按类别显示

总之,我不能在我的帖子模型中筛选具有外键模型(如Category、Author)的帖子。 我可以直接从帖子模型中筛选帖子,但不能从类别名称模型中筛选帖子标题

这意味着,当我尝试筛选Post.objects.all()时,我的所有帖子都会显示出来,但当我尝试筛选Post.objects.filter(category__name__in=[“news”])时,我的页面将不会显示任何内容,除非没有执行错误


帮助

这里的“新闻”中的空白是否有问题

另外,什么是
[0.5]
?我想你想要那里的
[0:5]


另外,确保
get\u queryset
get\u context\u data
方法正确缩进。因为如果它们没有您显示的缩进,那么它们在您的
PostListView
类之外定义…

因此,
Post.objects.filter(category\uu name\uu in=[“news”])
不返回任何帖子,表示您没有任何此类类别名称的帖子?我的数据库中有很多与新闻类别相关的帖子,但它仍然没有返回为post提供的异常,但它返回的是ahell上的帖子列表,而不是post list上的帖子列表,但是你看到我的代码有什么问题吗?请清理你问题中的代码,使其与你的问题一模一样。我看到一些奇怪的事情,如
返回上下文***
排序=[“日期发布”
无右括号,
发布=获取对象或404(发布)
[0.5]
由于数组索引,这个答案确实解决了我的问题,非常感谢各位,这确实是我错误地犯了[0.5]而不是[0:5]的空白错误或疏忽错误,先生,谢谢您的帮助
  <div> <h1>News</h1> <div/>  
{% for post in new_cat%} 
  div class="mt-2" 
 <a href="{{ post.get_absolute_url }}"><img class="object-cover w-full rounded-lg" src="{{ post.image.url }}" alt="{{ post.title }} cover"> </a></div>
<div class="py-2 flex flex-row items-center justify-between">
        <a href="{{ post.get_absolute_url }}"><h1 class="hover:text-blue-600 text-gray-900 font-bold text-xl mb-2 py-2">{{ post.title }}</h1></a></div> '''
Post.objects.filter(category__name__in=[" news"])[0.5]