Skip to content
Advertisement

Trying to Build url path through DetailView , but am getting duplicate values in html page

I am starting to learn django. I want to create a directory site.

I want it to be: home page -> list of States -> List of Restaurant Types -> List of Restaurant names

I have ‘list of States’ as a generic.ListView and it works perfect. I tried making ‘List of Restaurant Types’ as a ListView as well but it wouldn’t pull any data in the html. Changing it to a DetailView it pulls the data but has duplicate entries. Is there a way to restrict it as unique outputs in either views.py or the restaurant_detail.html?

The current html code is:

<p><b>Restaurant SECTION</b></p>
{% for name in states.restaurant_name_set.all %}
<p>{{name.restaurant_types}}</p>
{% endfor %}

I get something like:

Fine Dining Buffet Buffet Buffet Food Truck

I want just one of each I can then link to go to a list of Restaurant Names

Advertisement

Answer

Don’t use extra input parameters, just use the “ifchanged” Django built-in filter: https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#ifchanged

{% for name in states.restaurant_name_set.all|dictsort:'restaurant_name' %}
    <p>{% ifchanged %}{{name.restaurant_types}}{% endifchanged %}</p>
{% endfor %}
Advertisement