Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.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上显示类别标题_Python_Django - Fatal编程技术网

Python 如何在django上显示类别标题

Python 如何在django上显示类别标题,python,django,Python,Django,我的问题是,在分类页面中,标题、元标题和元描述不显示 我在下面上传的图片中展示了它 我怎么能修好它 多谢各位 模版 class Category(models.Model): parent = models.ForeignKey('self', blank=True, null=True, related_name='children', on_delete=models.CASCADE) title = models.CharField(max_length=70, uniqu

我的问题是,在分类页面中,标题、元标题和元描述不显示

我在下面上传的图片中展示了它

我怎么能修好它

多谢各位

模版

class Category(models.Model):
    parent = models.ForeignKey('self', blank=True, null=True, related_name='children', on_delete=models.CASCADE)
    title = models.CharField(max_length=70, unique=True)
    slug = models.SlugField(max_length=70, unique=True)
    description = RichTextUploadingField(blank=True, null=True)
    meta_title = models.CharField(max_length=120, unique=True, blank=True, null=True)
    meta_description = models.TextField(max_length=175, blank=True, null=True)

    def __str__(self):
       return self.title
    
    class MPTTMeta:
       order_insertion_by = ['title']
views.py

class PostCategoryView(generic.ListView):
    template_name = 'posts_category.html'
    paginate_by = 9

    def get_queryset(self):
        slug = self.kwargs.get('slug')
        return Post.objects.filter(category__slug=slug, status=1)

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['category'] = Category.objects.all()
        return context
类别模板

{% extends 'shared/_MainLayout.html' %}
{% load static %}
{% block head %}
{% block title %}**{{ category.meta_title }}**{% endblock %}
{% block description %}**{{ category.meta_description }}**{% endblock %}
{% endblock %}
{% block content %}
                <div class="page-header page-header-style-1 text-center mt-50">
                    <div class="container">
                        <h2><span class="color2">**{{ category.title }}**</span></h2>
                        <div class="breadcrumb">
                            <a href="/" rel="nofollow">Home</a> &nbsp; > &nbsp;
                            <span></span><a href="/{{ category.slug }}/" rel="nofollow">**{{ category.title }}**</a>
                        </div>
                    </div>
                </div>
{% endblock %}
{%extends'shared/\u MainLayout.html%}
{%load static%}
{%block head%}
{%block title%}**{{category.meta_title}}**{%endblock%}
{%block description%}**{{category.meta_description}}**{%endblock%}
{%endblock%}
{%block content%}
**{{category.title}}**
>  
{%endblock%}

您可以设置
上下文对象名称
来访问模板中的对象。或者您可以使用默认名称{{object_list}

例如:

class PostCategoryView(generic.ListView):
    template_name = 'posts_category.html'
    paginate_by = 9
    context_object_name = Example
    model = Category

    def get_queryset(self):
        slug = self.kwargs.get('slug')
        return Post.objects.filter(category__slug=slug, status=1)

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['category'] = Category.objects.all()
        return context
现在,您可以在模板中使用此选项:

{% for category in Example %}
    <li>{{ category.title }}</li>
{% endfor %}
{%用于示例%]中的类别
  • {{category.title}}
  • {%endfor%}
    如果未设置上下文\u对象\u名称,则可以使用默认名称“对象\u列表”,如下所示:

    {% for category in object_list %}
        <li>{{ category.title }}</li>
    {% endfor %}
    
    {%用于对象列表%中的类别]
    
  • {{category.title}}
  • {%endfor%}

    您还必须在列表视图中设置您的模型,例如:
    model=Category

    Category
    是一个查询集,而不是
    Category
    模型的实例,因此您需要迭代它才能得到每个对象的结果。我不明白,如果可能,请在代码上显示当我设置context\u object\u name时,当我设置context\u object\u name时,会发生错误,但它适用于object\u list和{{category.category.title}。非常感谢!如果您需要有关该错误的帮助,请让我知道并在此处发布您的错误。谢谢,但类别标题重复,例如,我们在该类别中有4篇文章,标题上重复了4次类别标题:(