Django loop through list - python

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 %}

Related

Django: Nested Loop In Template

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 %}

Shorten the construction

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?

How to pass template variable to slice filter in Django templates

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 %}

How can I loop through all the properties of an python object in django templates

I have django template and I get a some object.
I want apply for in cycle for all atributes this object.
{% for point in Object %}
<h1>{{ Object[point] }}</h1>
{% endfor %}
You can't do that in the template.
One solution is to convert your object in a dictionary in the view with object.__dict__ and then iterate over it with:
{% for attr, value in object_dict.items %}
{{ attr }} : {{ value }}
{% endfor %}
As py_dude suggests, you can discard attributes starting with underscores to keep your actual attributes.
{% for k, v in Object.__dict__.items() %}
{% if not k.startswith("_") %}
<h1>{{ v }}</h1>
{% endif %}
{% endfor %}

Django templates check whether key is existing

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 %}

Categories