I have a OrderedDict and I need to show its key, value but I am unable to get its value, I have this dict = ([('sainbath', 'thesailor'), ('HELLO', 'WORLD')]), I have tried this
{{object.specifications}}
<ul>
<li>{% for key in object.specifications %}
{{key}}
{%endfor%}
I am getting this output
OrderedDict([('sainbath', 'thesailor'), ('HELLO', 'WORLD')])
sainbath HELLO
I am getting its key only not value, when I tried this
{{object.specifications}}
<ul>
<li>{% for key,value in object.specifications %}
{{key}} : {{value}}
{%endfor%}
it give me error
Need 2 values to unpack in for loop; got 8.
please tell me how can I get value?
1) Use following in template:
{% for key,value in object.specifications.items %}
{{key}}:{{value}}
{% endfor %}
OR
2)If you are rendering a template with the data as OrderedDict, you can use following approach for the same.
return render_to_response('your_template.html',{'data': sorted(your_ordered_dict.iteritems())})
and in Template you can use the same as below:
{% for key, value in data %}
{{key}}:{{value}}
{% endfor %}
Hope it will help you!!
Related
In my template I have
<p>{% for dict_item in MySQL_Dict %}
{% for key, value in dict_item.items() %}
{{value}}
{% endfor %}
{% endfor %}</p>
Which outputs 43.8934276 -103.3690243 47.052060 -91.639868 How do I keep values i.e. the coordinates together AND have them separated by a comma? Is this possible?
The easiest way is to format it on back-end and pass it the jinja in the way you want. In fact its better to put complex logic on the back-end not the front-end performs better and in jinja's case you don't have to pass in python methods so it's cleaner... other templating frameworks like tornados include all of pythons base functions so you don't have to pass them in like the example below. Another way to do it is to pass the str.join, len, and range method to jinja e.x join=str.join, len=len, range=range then... and I'm guessing MySQL_Dict is a 2d array, hence the double for loop
<p>
{% for dict_item in MySQL_Dict %} // dict_item == [{}, {}, ...]
{% for i in range(len(dict_item)) %} // dict_item[i] == {some object}
{{",".join(dict_item[i]["latitude"], dict_item[i]["longitude"])}}
{% endfor %}
{% endfor %}
</p>
I need to display the value of list in template page using django. I have a variable like
data = [('dic1',),('dic2',)]
I pass the value to the template this way:
return render(request,'success.html',{'details':data})
Then in the template I need to show the value. I have used the for loop in the template like:
{% for a in data %}
{{a}}
{% endfor %}
But it displays as
('dic1',)
('dic2',)
when I need to display only the value like below
dic1
dic2
Canyone help me to find out which mistake I did ?
Thanks for your response. I jus to use like below
{% for a in data %}
{{ a.0 }}
{% endfor %}
Now it's working correctly.
My today's problem is that I've to preserve some url arguments that comes to my view in the request.
Think about the following url:
http://localhost:8000/some-url/?myargument=hello&myargument=world&myargument=whatever
Noticed that every argument in my url has the same key ("myargument")? Keep that in mind
In Django 1.6 if I do {{ request.GET }} in the template I get something like:
<QueryDict: {u'myargument': [u'hello', u'world', u'whatever']}>
To preserve the arguments in the next submit I want to iterate over the request.GET dictionary by creating a hidden field inside the <form> with the key-value pairs, I can do it with this code:
{% for key, value in request.GET.items %}
<input type="hidden" name="{{ key }}" value="{{ value }}">
{% endfor %}
It works with single values, but not in my case because I've a list with the values of the key named "myargument".
Obviously the first thing I tried was to iterate over the value by doing {% for v in value %} when the value is a list, but this only prints out the last item in the "value" list, in this case "whatever".
Anyone had the same problem? How can I solve it? Thanks
in the view:
mydict = dict(request.GET._iterlists())
sent and iterate in the template:
<p>
{{ mydict }}
</p>
<p>
{% for k,v in mydict.iteritems %}
{{ k }}:{% for x in v %}{{ x }},{% endfor %} <br>
{% endfor %}
</p>
I am retrieving model object to my template and I am getting it like this:
value: [{'name_value9': 48}]
When filter the object in the view I am using '.values' like this:
...
name_value.values('name_value' + str(fn))
...
The reason for that is that I have 'name_value1, name_value2, etc... and I need to get to the correct one.
How can I show in the template only the result (value) without the key (name_value9)?
Thanks.
You can print values in your template like this:
{% obj in obj_list %}
{% for key, value in obj.items %}
{{ value }}
{% endfor %}
{% endfor %}
I have a dictionary called number_devices I'm passing to a template, the dictionary keys are the ids of a list of objects I'm also passing to the template (called implementations). I'm iterating over the list of objects and then trying to use the object.id to get a value out of the dict like so:
{% for implementation in implementations %}
{{ number_devices.implementation.id }}
{% endfor %}
Unfortunately number_devices.implementation is evaluated first, then the result.id is evaluated obviously returning and displaying nothing. I can't use parentheses like:
{{ number_devices.(implementation.id) }}
because I get a parse error. How do I get around this annoyance in Django templates?
Thanks for any help!
A workaround could be using the keys from number_devices and check in the for loop if it is equal to the key provided by number_devices.
{% for key in number_devices.keys %}
{% for implementation in implementations %}
{% ifequal key implementation.id %} you got it {% endifequal %}
{% endfor %}
{% endfor %}
Seems a bit ugly, but should work.