Error I get:
Need 2 values to unpack in for loop; got 1.
Here is my view:
class Index(View):
def get(self, request, slug):
test = {
1: {
'id': 1,
'slug': 'test-slug-1',
'name': 'Test Name 1'
},
2: {
'id': 2,
'slug': 'test-slug-2',
'name': 'Test Name 2'
}
}
context = {
'test': test
}
return render(request, 'wiki/category/index.html', context)
Here is my template:
{% block content %}
<div>
{{ test }}
<ul>
{% for key, value in test %}
<li>
{{ key }}: {{ value }}
</li>
{% endfor %}
</ul>
</div>
{% endblock %}
I also tried the template like:
{% block content %}
<div>
{{ test }}
<ul>
{% for value in test %}
<li>
{{ value }}: {{ value.name }}
</li>
{% endfor %}
</ul>
</div>
{% endblock %}
No error then, but {{ value }} shows key (which is fine), but {{ value.name }} shows nothing. While {{ test }} shows my dict.
Loop through the dictionary's items to get the keys and values:
{% for key, value in test.items %}
Not familiar with Django. However, by default, Python iterates over the keys for a dictionary. I am also going to assume you are using Python2. To get the values, you need to do:
{% for value in test.itervalues() %}
If you want both, you need to do:
{% for key, value in test.iteritems() %}
That will give you both the key and the value.
Related
I'm trying to browse this array and display it on my django project but it doesn't work.
Views.py
def(request):
{"product":[
{
"name":"sogi",
"desc":"solo"
},
{
"name":"molo",
"desc":"kanta"
},
]
}
context={"tab":"product"}
return render(request,'api/myapi.html',context)
myapi.html
{% for pro in tab %}
{{pro.name}}
{% endfor %}
You have to change your context creation:
def(request):
tab = {"product": [...]}
products = tab["products"]
context = {"products": products}
return render(request, "api/myapi.html", context)
And change usage in your template:
{% for product in products %}
{% for key, value in product.items %}
{{ key }}: {{ value }}<br>
{% endfor %}
{% endfor %}
I'm trying to display values from a dictionary within a dictionary in a template using django.
I have a dictionary like this in my views:
characters = {
"char1": {'name': "David",
'stars': 4,
'series': "All star"},
"char2": {'name': "Patrick",
'stars': 3,
'series': "Demi god"}
}
I can display the whole dictionary on the page, however I want to display only the 'name' and 'David' key:value pairs. I wrote the following in the template:
{% for char in characters %}
{% for key, value in char %}
{{ key }}: {{ value }}
{% endfor %}
{% endfor %}
However this doesn't show me anything. What is wrong with this double loop?
Thanks
You have to add .items when you loop through key value pairs.
See below (Python 3):
{% for char in characters.items %}
{% for c in char %}
name: {{ c.name }}
{% endfor %}
{% endfor %}
In Python 2 it would be .iteritems
{% for char in characters.iteritems %}
{% for c in char %}
name: {{ c.name }}
{% endfor %}
Thanks to kfarnell's help I finally managed to get this:
{% for character, params in characters.items %}
{{ params.name }}: {{ params.stars }}
{% endfor %}
I am rendering a queryset on views.py like this:
person = MyDict.objects.filter(search_description = name)
return render(request,'myPage/find.html',{'person':person})
Its rendering like this:
person=[{
'gender': 'male',
'description': ['24', 'Student', 'NY']
}]
If i apply the following code on my html:
{% for item in person %}
{{ item.description }}
{% endfor %}
It returns as ['24', 'Student', 'NY']
But I want to view as like this:
24
Student
NY
How to do it???
You can use join template tag of django
{{ item.description|join:"<br>" }}
You nee to make templatetags for check value type is list or not.
templatetags code:
from django import template
register = template.Library()
#register.simple_tag
def is_list_type(data):
return isinstance(data, list)
html code:
{% load <templatetag file name> %}
{% for item in person %}
{% is_list_type item as result %}
{% if result %}
{% for value in item %}
{{value}} <br>
{% endfor %}
{% else %}
{{ item}}<br>
{% endif %}
{% endfor %}
Output:
male
24
Student
NY
I have following JSON document:
{
"document": {
"section1": {
"item1": "Item1Value",
"item2": "Item2Value"
},
"section2": {
"item1": "Item1Value",
"item2": "Item2Value"
},
"section3": {
"item1": "Item1Value",
"item2": "Item2Value"
}
}
}
Which I want to display in my django template. This document will be dynamic, so I want to display each sections dynamically. I am passing parsed (by json.loads()) string to the template.
I tried something like:
{% for section in json.document %}
<div class="section">
<h4> {{ section|title }}:</h4>
<table class="table table-condensed">
{% for field in json.document.section %}
{{ field }} {# <---- THIS should print item1, item2... Instead, its printing "section1" letter by letter etc #}
{% endfor %}
</table>
</div>
{% endfor %}
But it doesn't printting section's items properly. Any help?
You can instead pass the dictionary to your template and access it by iterating over the values using dict.items in your template.
{% for key1,value1 in json.document.items %}
<div class="section">
<h4> {{ key1|title }}:</h4>
<table class="table table-condensed">
{% for key2,value2 in value1.items %}
{{ key2 }}
{% endfor %}
</table>
</div>
{% endfor %}
In your code above, it was printing "section1" letter by letter because you were not iterating over the values of the section1 key but on the section1 string itself. If you need to access the items in a dictionary, you need to use individual variables for keys and values and use dict.items.
For example, the below code would print the keys and values of the data dictionary in the template.
{% for key, value in data.items %}
{{ key }}: {{ value }}
{% endfor %}
this is my views.py :
a=[{'s':'sss'},{'s':'wwww'},{'s':'ssaawdw'},{'s':'qqqww'}]
def main(request, template_name='index.html'):
context ={
'a':a,
}
return render_to_response(template_name, context)
this is my filter :
def return_next_element(list, index):
if(index<len(list)-1):
return list[index+1]
else :
return 0
register.filter('return_next_element',return_next_element)
and this is my template :
{% load myfilters %}
{% for i in a %}
{{ i }}ww{{ (a|return_element:forloop.counter0).s }}
{% endfor %}
but , this cant get the a.s ,
so what can i do ,
thanks
updated
this is not a same question , because i will use like this :
a|return_element:forloop.counter0).s|other filter
For your situation, I'd suggest doing the following:
{% for dictionary in a %}
{% for key, value in dictionary.iteritems %}
{{ key }}ww{{ value }}
{% endfor %}
{% endfor %}
But to answer your question, use the with tag.
http://docs.djangoproject.com/en/dev/ref/templates/builtins/#with
{% with a|return_element:forloop.counter0 as result %}
{{ result|other_filter }}
{% endwith %}