如何在Django模型中列出项目?

时间:2022-02-13 21:52:44

Am working with django Publisher example, I want to list all the publishers in the db via my list_publisher.html template, my template looks like;

我正在使用django Publisher示例,我想通过我的list_publisher.html模板列出数据库中的所有发布者,我的模板看起来像;

{% extends "admin/base_site.html" %}
{% block title %}List of books by publisher{% endblock %}
{% block content %}

<div id="content-main">
<h1>List of publisher:</h1>

{%regroup publisher by name as pub_list %}


{% for pub in pub_list %}


<li>{{ pub.name }}</li>

{% endfor %}
</div>
{% endblock %}

but when I run "http://127.0.0.1:8000/list_publisher/" the template just prints the page title with no error! What am I doing wrong?

但是当我运行“http://127.0.0.1:8000/list_publisher/”时,模板只打印页面标题而没有错误!我究竟做错了什么?

2 个解决方案

#1


3  

A few suggestions:

一些建议:

  • check that your base_site.html does define a {% block content %}{% endblock %} section to be refine by your my list_publisher.html
  • 检查您的base_site.html是否定义了{%block content%} {%endblock%}部分,以便通过我的list_publisher.html进行优化

  • check the cardinality of your list: {%regroup publisher by name as pub_list %}{{ pub_list|length }}. That should at least display the length of your list. If is is '0'... you know why it does not display anything
  • 检查列表的基数:{%reg publisher publisher by name as pub_list%} {{pub_list | length}}。这应该至少显示列表的长度。如果是'0'......你知道它为什么不显示任何东西

  • check that your list is indeed sorted by name before using regroup, or use a {% regroup publisher|dictsort:"name" by name as pub_list %} to be sure
  • 在使用重新组合之前检查您的列表是否确实按名称排序,或者使用{%regroup publisher | dictsort:“name”作为名称为pub_list%}以确保

If the length is '0', you have to make sure publisher is defined (has been initialized from the database), and sorted appropriately.

如果长度为“0”,则必须确保定义了发布者(已从数据库初始化),并进行适当排序。

In other word, do you see anywhere (in your template or in the defined templates):

换句话说,您是否在任何地方(在模板中或在定义的模板中)看到:

publisher = Publisher.objects.all().order_by("name")

?
(again, the order by name is important, to ensure your regroup tag works properly)

? (同样,按名称排序很重要,以确保您的重组标签正常工作)

#2


0  

Good answer by VonC.

VonC给出了很好的答案。

A quick and dirty way to look at pub_list is to stick [{{pub_list}}] in your template. I put it in square brackets in case it's empty. BTW, you may get something that looks like [,,,,,]. This is because object references are wrapped in <> and your browser is going WTF? Just do a View Source and you'll see the full result.

查看pub_list的快速而肮脏的方法是在模板中粘贴[{{pub_list}}]。我把它放在方括号中,以防它是空的。顺便说一下,你可能会得到一些看起来像[,,,,,]的东西。这是因为对象引用包含在<>中并且您的浏览器正在进行WTF?只需查看源代码,您就会看到完整的结果。

#1


3  

A few suggestions:

一些建议:

  • check that your base_site.html does define a {% block content %}{% endblock %} section to be refine by your my list_publisher.html
  • 检查您的base_site.html是否定义了{%block content%} {%endblock%}部分,以便通过我的list_publisher.html进行优化

  • check the cardinality of your list: {%regroup publisher by name as pub_list %}{{ pub_list|length }}. That should at least display the length of your list. If is is '0'... you know why it does not display anything
  • 检查列表的基数:{%reg publisher publisher by name as pub_list%} {{pub_list | length}}。这应该至少显示列表的长度。如果是'0'......你知道它为什么不显示任何东西

  • check that your list is indeed sorted by name before using regroup, or use a {% regroup publisher|dictsort:"name" by name as pub_list %} to be sure
  • 在使用重新组合之前检查您的列表是否确实按名称排序,或者使用{%regroup publisher | dictsort:“name”作为名称为pub_list%}以确保

If the length is '0', you have to make sure publisher is defined (has been initialized from the database), and sorted appropriately.

如果长度为“0”,则必须确保定义了发布者(已从数据库初始化),并进行适当排序。

In other word, do you see anywhere (in your template or in the defined templates):

换句话说,您是否在任何地方(在模板中或在定义的模板中)看到:

publisher = Publisher.objects.all().order_by("name")

?
(again, the order by name is important, to ensure your regroup tag works properly)

? (同样,按名称排序很重要,以确保您的重组标签正常工作)

#2


0  

Good answer by VonC.

VonC给出了很好的答案。

A quick and dirty way to look at pub_list is to stick [{{pub_list}}] in your template. I put it in square brackets in case it's empty. BTW, you may get something that looks like [,,,,,]. This is because object references are wrapped in <> and your browser is going WTF? Just do a View Source and you'll see the full result.

查看pub_list的快速而肮脏的方法是在模板中粘贴[{{pub_list}}]。我把它放在方括号中,以防它是空的。顺便说一下,你可能会得到一些看起来像[,,,,,]的东西。这是因为对象引用包含在<>中并且您的浏览器正在进行WTF?只需查看源代码,您就会看到完整的结果。