The challenge is to reduce the construction and, if possible, speed up the processing of the code.
{% for tp1 in test.tab_tp1s %}
{{ tp1.name }}
{% endfor %}
{% for tp2 in test.tab_tp2s %}
{{ tp2.name }}
{% endfor %}
{% for tp3 in test.tab_tp3s %}
{{ tp3.name }}
{% endfor %}
{% for tab in test %}
{% for tp in test[tab]
{{ tp.name }}
{% endfor %}
{% endfor %}
Maybe, it's hard to tell because we don't know what is stored in test, I'm assuming test is a dictionary holding 3 keys which each hold a sub key called name?
Related
category = ['cat1','cat2','cat3']
inventory = CurrentInventory.objects.all()
for cats in categories
inventorybycat = inventory.filter(category =cats)
setofinventories.append(inventorybycat)
dictofstuff = [zip(setofinventories,categories)]
context = {
'setofinventories':setofinventories
'category':category
'dictofstuff':dictofstuff
}
In views.py above this loop creates a list of objects per each category.
In the template below this loop prints a filtered object list per every item in the category list.
{% for inventory in setofinventories%}
{% for item in inventory %}
{{ item.category }}
{{ item.productName }}
{% endfor %}
{% endfor %}
The only thing I am missing is I do not now how to reference the category in the template. I pass the whole list in context, but {{category{{forloop.counter}}}} is not a valid statement.
I would either like to use zip(category,setofinventories) to pass these two items together,
or create a category model, filter by that model and then I can reference that model by item?
If I zip these items dictofstuff = [zip(setofinventories,categories)]
How do I reference the category in the template?
{% for inventory,categories in dictofstuff %}
{% for inventory in setofinventories%}
{% for item in inventory %}
{{ item.quantity }}
{{ item.productName }}
{% endfor %}
{% endfor %}
{% endfor %}
Returns Error: "Need 2 values to unpack in for loop: got 1."
If i right understand you, you want to print category only one time.
In your case:
{% for inventory in setofinventories%}
{% ifchanged inventory.category %}
{{ inventory.category }}
{% endifchanged %}
{% for item in inventory %}
{{ item.quantity }}
{{ item.productName }}
{% endfor %}
{% endfor %}
i use ifchanged, more here:
https://docs.djangoproject.com/en/4.1/ref/templates/builtins/#ifchanged
in views i zipped this to a *listofstuff. Forgive the nomenclature.
dictofstuff = [zip(categories,setofinventories)]
in template. Again, forgive bad nomenclature. I should go back and rename these but it works.
{% for category in dictofstuff %}
{% for item in category %}
{{item.0}}
{% for stuff in item %}
{% for thing in stuff %}
{{thing.productId}}{{thing.productName}}
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}
I'm trying to slice loop in django template with variable
USUAL WAY
{% for article in module.module_article_key.module_article_category.article_category_key.all|slice:":2" %}
{{ article.article_title }}
{% endfor %}
WHAT NEEDS
{% for article in module.module_article_key.module_article_category.article_category_key.all|slice:":module.module_article_key.module_article_count" %}
{{ article.article_title }}
{% endfor %}
so we have working variable {{ module.module_article_key.module_article_count }}
normaly this variable gives integer value stored for this module, however wen i use it to slice loop - nothing happens
You need to cast module_article_count to string first then making articleSlice via nested {% with %} and use the resulting template variable in slice filter as follow:
{% with articleCount=module.module_article_key.module_article_count|stringformat:"s" %}
{% with articleSlice=":"|add:articleCount %}
{% for article in module.module_article_key.module_article_category.article_category_key.all|slice:articleSlice %}
{{ article.article_title }}
{% endfor %}
{% endwith %}
{% endwith %}
So I am trying to render my models in a template but just one tiny thing shows more times than I expect. I just want category name to show one time for many lectures. I just cant figure where to modify the template code.
<ul>
{% for a in categories %}
{% for c in lectures %}
{% if a == c.course_category %}
<li><strong>{{ a.course_category }}</strong></li>
{% endif %}
{% if a == c.course_category %}
<li>{{ c.lecture_title }}</li>
<li>{{ c.content }}</li>
{% if c.link %}
<li>{{ c.link }}</li>
{% endif %}
{% if c.file %}
<li><a href='{{ MEDIA_URL }}{{ c.file.url }}'>download</a></li>
{% endif %}
{% endif %}
{% endfor %}
<hr/>
{% endfor %}
</ul>
You should move the {{ a.course_category }} out of the inner loop, so that you only display it once for each category.
{% for a in categories %}
<li><strong>{{ a.course_category }}</strong></li>
{% for c in lectures %}
<li>{{ lecture.lecture_title }}</li>
{% endfor %}
{% endfor %}
However, it's inefficient to loop through every lecture for every category. Depending on your models, you should be able to do something like:
{% for category in categories %}
<li><strong>{{ category.course_category }}</strong></li>
{% for lecture in category.lecture_set.all %}
<li>{{ lecture.lecture_title }}</li>
{% endfor %}
{% endfor %}
Or you might be able to loop through the lectures, and use the {% ifchanged %} tag to display the categories.
{% for lecture in lectures %}
{% ifchanged lecture.course_category %}
<li><strong>{{ lecture.course_category }}</strong></li>
{% endifchanged %}
<li>{{ lecture.lecture_title }}</li>
{% endfor %}
How do you loop trough a list in Django templates with a for loop?
my problem is with trying to fetch list.0 list.1 list.2
But when running the code i don't get anything
{% for i in range %}
{% for key, value in list.i.items %}
{{ key }}
{{ value }}
{% endfor %}
{% endfor %}
where i should be list.0-2 but it doesn't show anything on screen
If it is just a list containing a dict?
Just use the following code
{% for element in lst %}
{% for key, value in lst.items %}
{{ key }} --> {{ value }}
{% endfor %}
{% endfor %}
Urgently needed solution
I have dictionary say
dictcontents = {"product1":{"subproduct1":["value1","value2"]},"product2":{"subproduct2":["value3","value4"]}}
I have sent dictcontents to template .I want to iterate the dictionary in template and check the key existence dictcontents["product1"]["subproduct1"] and get the value of that checked key
where "subproduct1" key is intialised value by user. I need to access the information whether the user has entered the "subproduct1" value or not manually?
Thanks in advance
You can do this:
{% for key, val in d.items %}
{% if val.subproduct1 %}
{% for value in val.subproduct1 %}
{{ value }}
{% endfor %}
{% endif %}
{% if val.subproduct2 %}
{% for value in val.subproduct2 %}
{{ value }}
{% endfor %}
{% endif %}
{% endfor %}
or this:
{% for key, val in d.items %}
{% for key2, val2 in val.items %}
{% if val2 %}
{% for value in val2 %}
{{ value }}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}