How to use forloop counter within curly braces in django? - python

I was trying to parse following object in my django template:-
obj=[{'abc1',123},{'abc2',234}]
I was trying the following code:-
{% for me in obj %}
{{ me{{forloop.counter}} }}
{% endfor %}
I need to use the forloop counter to get abc1, abc2 values. Its throwing following error:-
could not parse the remainder: '{{forloop.counter'
Any help?

you do not need inner {{...}} brackets - Django is parsing variables in outer brackets already.
Also if you need to concatenate two values you need to use add filter
Then it should be:
{% for me in obj %}
{{ me|add:forloop.counter }}
{% endfor %}

Related

Using split in a django template

I have a dictionary passed to an html django template:
return render_to_response('showdata.html', context_instance=RequestContext(request, {'dictdati': context} ))
this dictionary has this structure:
{0: 'TO;DEC;1;2012/02/28 15:39:06.000;TO;1234;SI;DI;1234;TO;1\n', 1: 'TO;DEC;1;2012/02/28 15:39:06.000;TO;567;SI;DI;567;TO;1\n'}
and in an html template I need to print some of the values in the each row of the dict. If in a python file I use
for key,val in dictdati.items():
print val.split(';')[0]
it prints the first value correctly (TO) but in an html template
{% for key,val in dictdati.items %}
{{ val.split[0] }}
{% endfor %}
in the browser I receive the error:
TemplateSyntaxError at /getdata/
Could not parse the remainder: '[0]' from 'val.split[0]'
Can someone give me an idea to solve this problem?
you cant just call arbitrary logic in django... this is probably more of a job for the backend if you only want 0th but i think you can use the first filter
{{ my_var.split | first }}
I think I found solution so I made a custom Django filter
The Custom filter code:
from django import template
register = template.Library()
#register.filter
def get_index(value, arg):
return value[int(arg)]
And the HTML code:
{% for key,val in dict.items %}
{{ val|get_index:"0" }}
{% endfor %}
In get_index give the index number needed
Also, I see you have used string and then commas as values I have used it as a list so this code only works for value as list

Jinja python for loop syntax

I have a jinja code for python and its giving me an error it doesn't give me in python
{% for i, juice in enumerate(a['juice'] for a in television):};
alert({{ juice }});
{% endfor %};
The Error I'm getting is
expected token ',', got 'for'
You don't need to add : at the end of the for statements in Jinja2. And, you are not properly closing the tag - missing the % before the }.
Plus, there is no enumerate() function in Jinja2, use the loop.index0:
{% for a in television %}
{{ loop.index0 }}, {{ a["juice"] }}
{% endfor %}
If you want to use more Python in the templates, you should probably look at the Mako engine.

Creating a list from CSV value in Jinja

In a flask template, I'd like to loop over my values which is a comma separated range of values. So in my template, I'd like to do something like:
{% for tag in list(myparent.my_tags) %}
{{tag}}
{% endfor %}
I can see list in the documents, but I don't see how to use it. http://jinja.pocoo.org/docs/dev/templates/
The value of my_tags is abc, def, ghi,... and the aim to loop over each group in turn.
Jinja2's split function should work.
{% for tag in myparent.my_tags.split(',') %}
{{ tag }}
{% endfor %}

Accessing individual objects in a Django template file

I am editing a template to display an attribute of the first member in a list. I am trying to access it like so:
{{ food_list.index(0).id }}
I get an error that says Could not parse the remainder: '(0).id'.
What is the correct way to access an individual member in a list?
{{ food_list.0.id }}
Alternatively,
{% with f=food_list|first %}
{{ f.id }}
{% endwith %}
You can't call methods in a template. Either write a template tag for this, or do it in your view. If what you wanted to do was just index the list, then you don't use the index() method for that; just use normal dot notation instead.

Django dictionary in templates: Grab key from another objects attribute

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.

Categories