Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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,我使用通用视图列出我的类别。 我还想显示属于这些类别的每个项目的标题。 我理解ListView和DetailView的原理,但是列表中的一些细节呢 以下是我的不同文件: Models.py Views.py url.py Index.html 您需要首先反向调用具有相关名称(即类别)的详细信息 {% load staticfiles %} <p>These is a list of categories</p> {% for category in list_cate

我使用通用视图列出我的类别。 我还想显示属于这些类别的每个项目的标题。 我理解ListView和DetailView的原理,但是列表中的一些细节呢

以下是我的不同文件:

Models.py

Views.py

url.py

Index.html


您需要首先反向调用具有相关名称(即类别)的详细信息

{% load staticfiles %}

<p>These is a list of categories</p>

{% for category in list_categories %}
    <div class="article">
       <h3>{{ category.name }}</h3>

        {% for detail in category.categories.all %}
            <p> {{detail.title}} </p>
        {% endfor %}
    </div>
请注意,您必须使用all after reverse all,因为可能存在多个反向关系


仍然有任何疑问,请在下面进行评论。

我想您可能正在寻找查询。如果我的理解正确,您应该可以通过category对象上的属性查找属于某个类别的详细信息。请按如下所示更改详细信息模型的category属性,然后重试。category=models.ForeignKey'categories',on_delete=models.CASCADE,related_name=detailok,那么第一个category=models.ForeignKey'categories',on_delete=models.CASCADE,related_name=details,正如Chetan Ganji建议的那样,然后-是的,当您定义ForeignKey关系时,在类别和详细信息之间创建一对多关系。此外,在类别中还添加了一个属性。默认情况下,该属性是带有后缀_集(即详细信息集)的模型名称,但当您指定实际的_名称时,该名称将与相关的_名称相同,现在,您可以通过其与类别对象的相关名称轻松调用详细信息。我希望你现在能更清楚地理解它。
class IndexView(generic.ListView):
    model = categories
    context_object_name = "list_categories"
    template_name='show/index.html'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
]
{% load static %}

<p>These is a list of categories</p>

{% for category in list_categories %}
    <div class="article">
       <h3>{{ category.name }}</h3>

        {% for title in category.detail %}
            <p> {{title}} </p>
        {% endfor %}
    </div>
{% endfor %}
{% load staticfiles %}

<p>These is a list of categories</p>

{% for category in list_categories %}
    <div class="article">
       <h3>{{ category.name }}</h3>

        {% for detail in category.categories.all %}
            <p> {{detail.title}} </p>
        {% endfor %}
    </div>