How do I access the value of tuple through indexing in a template? When i try to do this....django gives me the error : Could not parse the remainder: '[0]' from 'tuple[0]'.
template.html:
{{ tuple[0] }}
views.py:
def fun(request):
tuple=('a','b','c','d')
return render(request,'template.html',{'tuple':tuple})
Simply access tuple as in code below:
{{ tuple.0 }}
Also consider using django built-in template tags to iterate over your data, see simple usage below:
{% for item in tuple %}
<span>
{{ item }}
</spam>
{% endfor %}
Related
I have an array ['one', 'two', 'three']
In my django template i want to access to elements of the array like that:
{% for a in array %}
{{ array.loop.counter}}
{% endif %}
But array.loop.counter return nothing.
There is a way to access the element of the array based on the loop counter of my for ?
Ok I find a way to do it.
create a template tag into templatetags repository.
Use this custom filter:
from django import template
register = template.Library()
#register.filter(name='index')
def index(sequence, position):
return sequence[position]
Then into the template :
{{ array|index:forloop.counter }}
Why not just do this?:
{% for a in array %}
{{ a }}
{% endif %}
In a Django template, I want to construct dynamically urls
I try to concatenate (see below) but got an error :
Could not parse the remainder: '+key+'_index'' from ''ecrf:'+key+'_index''
{% for key, values in forms.items %}
<!-- for exemple a key would be 'inclusion' -->
{{ key|capfirst }}
{% endfor %}
urls
app_name='ecrf'
urlpatterns = [
path('inclusion/create/', views.InclusionCreate.as_view(), name='inclusion_create'),
expected result:
Inclusion
You can use the add template tag[Django docs] to add strings together:
{% for key, values in forms.items %}
<!-- for example a key would be 'inclusion' -->
{{ key|capfirst }}
{% endfor %}
But this feels a bit hacky to me, perhaps your forms dictionary can be restructured so that such logic is done in the view itself? For example the dictionary can be:
{'inclusion': {'value': <some_value>, 'create_url': 'ecrf:inclusion_create'}}
And in your template your loop would be:
{% for key, values in forms.items %}
<!-- for example a key would be 'inclusion' -->
{{ key|capfirst }}
{% endfor %}
I'm collecting data in multiple nested dicts and passing them to my template. I wrote a simple filter to get the data from the dict by key:
#register.filter
def get_item(dictionary, key):
return dictionary.get(key)
In my template I'm accessing the data:
{% for location_type_id, location_type_name in location_types.items %}
{% with disctrict=disctricts|get_item:location_type_id %}
...
{% endwith %}
{% endfor %}
When I'm using {{ debug }} I see:
{'districts': {5: {6: 'Friedrichshain'}, 7: {7: 'Kreuzberg', 8: 'Charlottenburg'}},
'location_types': {5: 'Bar', 7: '5'}}
But I get the error: 'str' object has no attribute 'get'. When printing the arguments of the filter I get an empty string passed to filter instead of a dict.
There's a typo in your template, which probably causes the error:
{% with disctrict=disctricts|get_item:location_type_id %} should be {% with disctrict=districts|get_item:location_type_id %}
You have a typo. The variable name is districts but you are using disctricts in the template.
I am trying to print out the values inside the request.META in a template but I cannot get it right. All I got is an error Could not parse the remainder: '[i]' from 'REQ_META[i]'
below is my code:
in my views.py
def index (request):
template = loader.get_template('app/index.html')
page_data = { 'REQ_META': request.META}
context = RequestContext(request, page_data)
return HttpResponse(template.render(context))
in index.html
{% for i in REQ_META %}
{{ i }} = {{ REQ_META[i] }} <br />
{% endfor %}
Well, the right way to inspect the request.META objects would be using pdb in the view, or using tools like django-debugtoolbar.
In my opinion, django debug toolbar is an extremely handy tools for debugging purposes.
Regardless, Your issue is, REQ_META is a dictionary, and the way to parse the elements of the dictionary is:
{% for k, v in REQ_META %}
{{ k }} = {{ v }} <br />
{% endfor %}
Documentation here
There is already an answer, but thought it could be useful for future use:
You just need to access the object like this {{ REQ_META.i }} instead of {{ REQ_META[i] }}
Another option is to use django pprint template filter
{{ REQ_META|pprint }}
Which will always print nicely dict objects (and any other python object)
How can I get the value of form.field in template. I mean not the html input element of the field but the value inside the input?
To get the bound data (in 1.2.3)
{{ form.field.data }}
In the development version, it's {{ form.field.value }} which automatically pulls initial data OR bound data whereas it's an explicit call in the current release:
form.field.field.initial or form.field.data
Update: the admin forms work differently. First of all, the BoundField is {{ adminfield.field }} and not {{ adminfield }} in your comment, but we have bigger problems.
On the change form, the form is not bound so data can only be pulled from the initial dictionary passed into the form constructor. It's not accessible via the django template syntax.
Here's the relevant lines in BoundField:
if not self.form.is_bound:
data = self.form.initial.get(self.name, self.field.initial)
# you can't do this kind of lookup from the template.
The only way to access this type of info from the template without making a template tag (which you should!) is to loop through each key/value in the initial dictionary and comparing to the current fields name.
{% for line in fieldset %}
{% for adminfield in line %}
{% for k, v in adminfield.field.form.initial.items %}
{% if k == adminfield.field.name %}
{{ k }}:{{ v }}
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}