Question

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;

{% 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?

Was it helpful?

Solution

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
  • 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
  • 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

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

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)

OTHER TIPS

Good answer by 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top