I am getting an array arr passed to my Django template. I want to access individual elements of the array in the array (e.g. arr[0], arr[1]) etc. instead of looping through the whole array.
Is there a way to do that in a Django template?
Remember that the dot notation in a Django template is used for four different notations in Python. In a template, foo.bar can mean any of:
foo[bar] # dictionary lookup
foo.bar # attribute lookup
foo.bar() # method call
foo[bar] # list-index lookup
It tries them in this order until it finds a match. So foo.3 will get you your list index because your object isn't a dict with 3 as a key, doesn't have an attribute named 3, and doesn't have a method named 3.
arr.0
arr.1
etc.
You can access sequence elements with arr.0, arr.1 and so on. See The Django template system chapter of the django book for more information.
When you render a request to context some information, for example:
return render(request, 'path to template', {'username' :username, 'email' :email})
You can access to it on template, for variables
{% if username %}{{ username }}{% endif %}
for arrays
{% if username %}{{ username.1 }}{% endif %}
{% if username %}{{ username.2 }}{% endif %}
you can also name array objects in views.py and then use it as shown below:
{% if username %}{{ username.first }}{% endif %}
Related
In my Jinja template, model.DataType value can be user defined or built in. My requirenement is if model.DataType start with the three letters ARR, then do a specific operation.
Example of values:
ARRstruct124
ARR_int123
ARR123123
CCHAR
UUINT
etc.
{% set evenDataType = model.eventDataType %}
{%if evenDataType | regex_match('^ARR', ignorecase=False) %}
// do the operation
{%else%}
// do the operation
{% endif %}
With this template, I am getting the error
{%if evenDataType | regex_match('^ARR', ignorecase=False) %}
jinja2.exceptions.TemplateAssertionError: no filter named 'regex_match'
There is indeed no regex_match filter in the Jinja builtin filters. You might have found some examples using it, but this is an additional filter provided by Ansible, so it won't work outside of Ansible.
This said, your requirement does not need a regex to be fulfilled, you can use the startswith() method of a Python string.
So, you template should be:
{% set evenDataType = model.eventDataType %}
{% if evenDataType.startswith('ARR') %}
`evenDataType` starts with 'ARR'
{% else %}
`evenDataType` does not starts with 'ARR'
{% endif %}
i'm try to do smthing like this:
{% for i in list %} <td style="background-color: {{color_rule|get_item:i}}">{{i}}</td> {% endfor %}
where:
def get_item(dictionary, key): return dictionary.get(key)
please help
The dictionary keys are lower case letters but the list are all uppercase - keys are case sensitive so nothing is being returned. Change your list to all lower case or your dict keys to uppercase and it will work.
Referring to the comment with additional context:
color_rules = {
'c':'rgb(87,213,255)',
'a':'rgb(182,83,248)',
'g':'rgb(163,196,60)',
't':'rgb(243, 100, 96)',
},
list = "CAGCCAGACCACAGGCCAGACATGACGTGGAGGCAAGCGGCCACAACGTGGAGGTGGA"
As an aside, you should avoid naming variables with python functions, i.e. list() - also the list isn't actually a list but a string. Something like color_string would be more appropriate.
According to the documentation.
{% for key, i in list.items %}
{{ key }}: {{ i}}
{% endfor %}
I'm passing a dictionary in the context in my views.py to my html template. How do I know access a value in the template based on a particular key. For instance I'd wanna do something like {{ dictionary.keyname.value }} but I don't know the correct syntax and for some reason I can't seem to find the documentation.
I want to achieve the same effect as this without having to use a for loop:
<b>Calories</b>
{% for key, value in output.items %}
{% if key == "calories" %}
{{ value }}
{% endif %}
{% endfor %}
You just want {{ output.calories }}.
If I pass in a json object from my view i.e. return render(request, 'development/index.html', {'data': data}), where data is {a:{"colA":12,"colB":13"},b:{"ColA":1","colB":2"}}, how can I populate a select in index.html with all the values in ColA?
You should be populating selects with options using django forms, not in template.
But if you absolutely have to do it in template then you can loop through dict like this:
{% for item in data.iteritems %}
{{ item.colA }}
{% endfor %}
I have a todo model defined below:
class Action(models.Model):
name = models.CharField("Action Name", max_length=200, unique = True)
complete = models.BooleanField(default=False, verbose_name="Complete?")
reoccurance = models.ForeignKey(Reoccurance, blank=True, null=True, verbose_name="Reoccurance")
notes = models.TextField("Notes", blank=True)
tags = TaggableManager()
class Reoccurance(models.Model):
label = models.CharField("Label", max_length=50, unique = True)
days = models.IntegerField("Days")
I want to list all of the actions that are incomplete:
actions = Action.objects.filter(complete=False)
My template loops of the actions list:
{% for action in actions %}
<p>{{ action }}</p>
{% if action.reoccurance %}
<p>{{ action.reoccurance }}</p>
{% endif %}
{% for tag in action.tags.all %}
<span>{{ tag }}</span>{% if not forloop.last %}, {% endif %}
{% endfor %}
{% endfor %}
Using django-debug-toolbar, I see that for every action, I'm hitting the database on {% if action.reoccurance %} and {% for tag in action.tags.all %}.
Is there a better way to write my query so that the database isn't pinged for every iteration of the loop? I think it has something to do with select_related, but I'm not sure what to do about django-taggit.
Update I got part of my answer. select_related does work, but I had to specify reoccurance, probably because I can't use it for tags:
actions = Action.objects.select_related('reoccurance').filter(complete=False)
The problem still remains that I hit the database for every "action.tags.all" in the template loop. Is it possible to use some sort of prefetch on django-taggit?
It's possible to use prefetch_related to retrieve the tags, but you need to sidestep around the 'tags' property, since - as jdi says - this is a custom manager rather than a true relation. Instead, you can do:
actions = Action.objects.select_related('reoccurance').filter(complete=False)\
.prefetch_related('tagged_items__tag')
Unfortunately, action.tags.all in your template code will not make use of the prefetch, and will end up doing its own query - so you need to take the rather hacky step of bypassing the 'tags' manager there too:
{% for tagged_item in action.tagged_items.all %}
<span>{{ tagged_item.tag }}</span>{% if not forloop.last %}, {% endif %}
{% endfor %}
(Ed.: if you're getting "'QuerySet' object has no attribute 'prefetch_related'", that suggests that you're on a version of Django below 1.4, where prefetch_related isn't available.)
The problem is that tags is not a field, but a custom manager, which lives at the class-level and simply does queries.
I am not sure if this will work on custom managers, as it is meant for many-to-many fields and the like that produce similar query sets. But if you are using django 1.4 you can try the prefetch_related. It will do one more query that batches out the relations and caches them.
Disclaimer again: I don't know if this works for managers
actions = Action.objects.select_related('reoccurance').filter(complete=False)\
.prefetch_related('tags')