[Answer]-Django templates, how to make a list of item's values from list of items

1👍

In your view, just add .values_list('name', flat=True) to your queryset. Now you are passing a list of item names to your template.

Other options:

  • Use a for loop to iterate over items.
  • Make a custom template tag.

0👍

If your just want to iterate the names in items:

{% for item in items %}
<p>name: {{item.name}}</p>
{% endfor %}

0👍

You can iterate over the items like that, in a for:

{% for item in items %}
    <p>Name: {{ item.name }}</p>
    <p>Value: {{ item.value }}</p>
{% endfor %}

You can’t “create” variables in the Django Template Engine, but you can do it in your View:
items_names = [x['name'] for x in items]

And then access the items_names in your template.

Leave a comment