I've reading all the template tags posts regarding the loop variable in the key. Apparently Django does not support loop variable in key and I am not sure how to use the custom template tag.
I wanted to display something like this, but how can I achieve this with {% for i in mData %} loop ?
{{ mData.0.name }}
{{ mData.1.name }}
{{ mData.2.name }}
{{ mData.0.age }}
{{ mData.1.age }}
{{ mData.2.age }}
mData is a list of dictionaries.
mData = { "name":"alex", "age":"12"},{"name":"amy","age":"14"} ...
Considering your data is in a list of dictionaries such as:
my_data = [{"name" : "abc" , "age":20,...}, {} , {}...]
You can access all attributes of each dictionary in your template this way:
{% for dict in my_data %}
<!-- Here dict would be each of the dictionary in my_data -->
<!-- You can access elements of dict using dot syntax, dict.property -->
{{ dict.name }}, {{ dict.age }}, ... {{ dict.property }}
{% endfor %}
Reference links: Django templating language
If you want to structure your elements in the order you specifed, you can do something like this:
Name List:
{% for dict in my_data %}
my_data.name
{% endfor %}
Age List:
{% for dict in my_data %}
my_data.age
{% endfor %}
...
Prpoerty List:
{% for dict in my_data %}
my_data.property
{% endfor %}
Django template tags are intentionally very lightweight so that you don't put too much code into the template itself. If you need to do something complicated like loop over every other entry in the database, you should be setting that up in views.py instead of the template.
For the scenario you described, all you need to do is loop over the list of objects:
{% for data in datas %}
{{ data.name }}
{% endfor %}
{% for data in datas %}
{{ data.age }}
{% endfor %}
This solved the problem for me
{% for d in mData %}
{{ d.name }} {{ d.age }}
{% endfor %}
{% for k, v in mData.items %}
{{ k }} {{ v }}
by the way, PEP8 suggest we name the variable as lower + _, but not hump like javascript or other languages.
Related
I don't really know how to phrase my question, but what I'm trying to do is add a title between items to split them into obvious categories on my template. I would like to do this:
{# set variable "current_category" to an empty value first #}
{% for item in items %}
{% if item.category != current_category %}
{{ current_category = item.category }} {# <-- How to do that? #}
<h1>{{ item.category }}</h1>
{% endif %}
<p>{{ item.name }}</p>
{% endfor %}
Then end up with:
<h1>Cat 1</h1>
<p>item</p>
<p>item</p>
...
<h1>Cat 2</h1>
<p>item</p>
..
I saw on similar answers that there are things called custom filters and simple tags but it seems really complicated for something very simple. Is this really the only way to do that?
NOTE: I already ordered the items by category of course
I think you're looking for Django's regroup template tag, which does exactly what you're asking for.
{% regroup items by category as grouped_items %}
{% for category, item_list in grouped_items %}
<h1>{{ category }}</h1>
{% for item in item_list %}
<p>{{ item.name }}</p>
{% endfor %}
{% endfor %}
I have a list of tuples called top_5 which are the top 5 users usernames and their corresponding post count.
This is a list that has 5 items and each item has 2 indexes.
{% for user in top_5 %}
{{ user }}
{% endfor %}
returns the user and his post count but is there a way I can get them separately?
Is this a Django template? You can access index values in Django template using the dot notation:
{% for user in top_5 %}
{{ user.0 }} {{ user.1 }}
{% endfor %}
And as mentioned in the comments, unpacking the tuples is also an option:
{% for username, post_count in top_5 %}
{{ username }} {{ post_count }}
{% endfor %}
I want to print value by id in database,And don't know which keywords to find in Google.
in my views.py, I send transen = TransEn.objects.all() to template
and this will print all datas from database:
{% for words in transen %}
{{words.words|safe }}
{% endfor %}
But I want to print by the value of the id Like:
(Because they are words in English for translating website)
I don't know how to write this in template, please guide me, Thank you very much.
<div><span> TransEn.objects.filter(id='2') </span></div>
<div> TransEn.objects.filter(id='3') </div>
UPDATE:
I have found a method:
I can use if tag, but are there another ideas??
<div>
{% for words in transen %}
{% if words.id == 2 %}
{{ words.words|safe }}
{% endif %}
{% endfor %}
</div>
<div>
{% for words in transen %}
{% if words.id == 3 %}
{{ words.words|safe }}
{% endif %}
{% endfor %}
</div>
If you want to access each item in the QuerySet individually, by index, you should cast it to a list first. You should change your views.py to:
transen = list(TransEn.objects.all())
And then in your template you can access them by index like so:
<div><span> {{ transen.1.words }} </span></div>
<div> {{ transen.2.words }} </div>
A warning from the Django docuemtnation about casting a QuerySet to a list:
Be warned, though, that this could have a large memory overhead, because Django will load each element of the list into memory. In contrast, iterating over a QuerySet will take advantage of your database to load data and instantiate objects only as you need them.
Taking simple regroup example from Django documentation:
{% regroup cities by country as country_list %}
<ul>
{% for country in country_list %}
<li>{{ country.grouper }}
<ul>
{% for item in country.list %}
<li>{{ item.name }}: {{ item.population }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
If country.grouper is a Char field declared in model with choices=CHOICES_FIELD, how can I display it's verbose name in template ? Normally i would take Model.get_FOO_display but country.grouper.get_country_list_display of course will not work.
Is custom template tag only choice ?
You simply have to look at it the other way around!
Use get_FOO_display as the grouping field.
{% regroup cities by get_country_display as country_list %}
{{ country.grouper }} will now display the value fields from the
choices set rather than the keys.
(taken verbatim from djangodocs)
I am trying to access a multidimensional dictionary in a Django template. I am able to view first level keys, but since second level keys I cannot see anything. In example dictionary is composed in this way:
dictionary = {}
dictionary[first_level] = {}
dictionary[first_level][second_level] = {}
...
and so on
From Django template I use:
{% for flk in dict %}
<!-- Using nested for from the following, no output is shown -->
{% for slk in dict.flk %}
<th>First level key : {{ flk }} Second level key : {{ slk }}</th>
{% endfor %}
<!-- -->
{% endfor %}
Have I to use a model or can I do it using this dictionary?
Thanks
I've found the solution on this page
Basically the code becomes
{% for flk, flv in dict.items %}
{% for slk, slv in flv.items %}
<th>First level key {{ flk }} Second level key {{ slk }}</th>
{% endfor %}
{% endfor %}
where each dictionary is decomposed in keys (flk, slk) and values (flv, slv).