How to use if/else condition on Django Templates? - django

I have the following dictionary passed to a render function, with sources being a list of strings and title being a string potentially equal to one of the strings in sources:
{'title':title, 'sources':sources})
In the HTML template I'd like to accomplish something among the lines of the following:
{% for source in sources %}
<tr>
<td>{{ source }}</td>
<td>
{% if title == {{ source }} %}
Just now!
{% endif %}
</td>
</tr>
{% endfor %}
However, the following block of text results in an error:
TemplateSyntaxError at /admin/start/
Could not parse the remainder: '{{' from '{{'
...with {% if title == {{ source }} %} being highlighted in red.

You shouldn't use the double-bracket {{ }} syntax within if or ifequal statements, you can simply access the variable there like you would in normal python:
{% if title == source %}
...
{% endif %}

Sorry for comment in an old post but if you want to use an else if statement this will help you
{% if title == source %}
Do This
{% elif title == value %}
Do This
{% else %}
Do This
{% endif %}
For more info see https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#if

{% for source in sources %}
<tr>
<td>{{ source }}</td>
<td>
{% ifequal title source %}
Just now!
{% endifequal %}
</td>
</tr>
{% endfor %}
or
{% for source in sources %}
<tr>
<td>{{ source }}</td>
<td>
{% if title == source %}
Just now!
{% endif %}
</td>
</tr>
{% endfor %}
See Django Doc

You try this.
I have already tried it in my django template.
It will work fine. Just remove the curly braces pair {{ and }} from {{source}}.
I have also added <table> tag and that's it.
After modification your code will look something like below.
{% for source in sources %}
<table>
<tr>
<td>{{ source }}</td>
<td>
{% if title == source %}
Just now!
{% endif %}
</td>
</tr>
</table>
{% endfor %}
My dictionary looks like below,
{'title':"Rishikesh", 'sources':["Hemkesh", "Malinikesh", "Rishikesh", "Sandeep", "Darshan", "Veeru", "Shwetabh"]}
and OUTPUT looked like below once my template got rendered.
Hemkesh
Malinikesh
Rishikesh Just now!
Sandeep
Darshan
Veeru
Shwetabh

Related

overriding Django change_list_results

I am customizing my admin panel a little and want to add an additional column to it, so as per the docs I have to override the change_form_results.html file but how do I do it? all I see is this code in it
<tbody>
{% for result in results %}
{% if result.form and result.form.non_field_errors %}
<tr><td colspan="{{ result|length }}">{{ result.form.non_field_errors }}</td> </tr>
{% endif %}
<tr>{% for item in result %}
{{ item }}
{% endfor %}</tr>
{% endfor %}
</tbody>
what change am i suppose to make to above code

Fixing ManagementForm-data

I have some code which looks like this:
<table>
{% for n in model %}
{% for i in formset.forms %}
{% if forloop.parentloop.counter == forloop.counter %}
<tr>
<th>{{ n }}</th>
{% for j in i %}
<th>{{ j }}</th>
{% endfor %}
</tr>
{% endif %}
{% endfor %}
{% endfor %}
</table>
I really want to keep it this way, because this is the presentation I've been asked for. However, I keep getting the "ManagementForm-data missing or tampered with" error, obviously because I'm messing with the formset.
Is there a smart way to fix the managementform-data so my POST will go through, or do I have to reformat my template completely?
(Yes, I am aware that my code contains an ugly, inefficient hack. Please feel free to suggest an alternative, but performance doesn't matter.)
You get the error about missing management form data because you haven't included the management form with {{ formset.management_form }}. See the docs for more info.
To prevent the double loop in the template, you can zip model and formset.forms in the view:
models_and_forms = zip(model, formset.forms)
Then loop through the models_and_forms in the template:
<table>
{{ formset.management_form }}
{% for n, i in models_and_forms %}
<tr>
<th>{{ n }}</th>
{% for j in i %}
<th>{{ j }}</th>
{% endfor %}
</tr>
{% endfor %}
</table>

django template view nested list

i made a nested list and from that id like to make a table in my template
list looks something like
ground_skills_available = [[category1, [skill1, skill2, skill3]], [category1, [skill1, skill2]]]
Now i want to list it that you got category with below the items, next category and other items. Problem is, i have no clue about hot to show only category, instead of category +all items, since you cant seem to use the index of a list?
Can someone please help me out?
<table>
{% for categories in ground_skills_available %}
{% for category in categories %}
<tr>
<td>{{ category }}</td>
</tr>
{% for skill in category %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}
Change your outer loop to
{% for category, skills in ground_skills_available %}
This technique is described in the Django for loop docs.
On the first iteration of the loop, this will take the list [category1, [skill1, skill2, skill3], and assign
category = category1
skills = [skill1, skill2, skill3]
You can then display {{ category }}, and loop through skills.
Putting that together, you have:
<table>
{% for category, skills in ground_skills_available %}
<tr>
<td>{{ category }}</td>
</tr>
{% for skill in skills %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
{% if forloop.index == 1 %} ... some code ... {% endif %}
or
{% if forloop.index0 == 0 %} ... some code ... {% endif %}
Expectation:
{% for categories in ground_skills_available %}
{% for category in categories %}
{% if forloop.first %}
<tr>
<td>{{ category }}</td>
</tr>
{% else %}
{% for skill in category %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}

How to I get the length or size or number of fields in a form in a django template?

I would like to write a loop like this, so that I can spread the form fields out in a table. :
{% load widget_tweaks %}
{% load mathfilters %}
{% load get_range %}
{% for k in form|length|div:5|floatformat|add:1|get_range %}
<tr>
{% for field in form %}
{% if forloop.counter >= k|mul:5 and forloop.counter <= k|mul:5|add:4 %}
<th>{{ field.label_tag }}{{ field.errors }}</th>
{% endif %}
{% endfor %}
</tr>
<tr>
{% for field in form %}
{% if forloop.counter >= k|mul:5 and forloop.counter <= k|mul:5|add:4 %}
<td>{{ field|add_class:"span4" }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
This doesn't work, but because the code above fails on form|length. In order for this to work, I need to get, in a template, the number of fields in a form. Does anyone know how to do this? I've searched all over but can't find anything. The following do NOT work:
form.len
form.length
form|length
Thanks!
I'm really not sure what you are looking for, but it sounds like this:
{% for field in form %}
<tr>
{% if forloop.counter0|divisibleby:5 %}
<th class="span4">{{ field.label_tag }}{{ field.errors }}</th>
{% else %}
<th>{{ field.label_tag }}{{ field.errors }}</th>
{% endif %}
</tr>
{% endfor%}
{% for field in form %}
<tr>
{% if forloop.counter0|divisibleby:5 %}
<td>{{ field|add_class:"span4" }}</td>
{% else %}
<td>{{ field }}</td>
</tr>
{% endfor %}
I dont like this code, but it was my first idea.
{% for field in form %}
{% if forloop.last %}
{{ forloop.counter }}
{% endif %}
{% enfor %}
form.fields I believe.
{% for field_name in form.fields %}
thanks for your suggestions - they helped! Here is what finally worked for me:
{% for field in form %}
{% if forloop.counter0|divisibleby:5 %}
<tr>
{% for field in form %}
{% if forloop.counter0 >= forloop.parentloop.counter0 and forloop.counter0 <= forloop.parentloop.counter0|add:4 %}
<th>{{ field.label_tag }}{{ field.errors }} </th>
{% endif %}
{% endfor %}
</tr>
<tr>
{% for field in form %}
{% if forloop.counter0 >= forloop.parentloop.counter0 and forloop.counter0 <= forloop.parentloop.counter0|add:4 %}
<td>{{ field }}</td>
{% endif %}
{% endfor %}
</tr>
{% endif %}
{% endfor %}

django form label in {% for %} statement

I want to wrap a in a label, but when the template was rendered, it didn't generate the right html, here is my code:
{% for item in studentinfo %}
<form action="" method="">
{% csrf_token %}
<tr>
<td>{{ item.nickname|default_if_none:"" }}</td>
<td>{{ item.mobile|default_if_none:"" }}</td>
<td>{{ item.register_at|date:"Y-m-d"|default_if_none:"" }}</td>
<td>{{ item.sales.first_name|default_if_none:"" }}</td>
<td><strong class="red-text">{{ item.price|default_if_none:"" }}</strong></td>
<td><strong class="red-text">{{ item.remaining|default_if_none:"" }}</strong></td>
<td>{{ item.level|default_if_none:"" }}</td>
<td>
{% if item.state %}
{{ setstudentform.state|default:item.state }}
{% else %}
{{ setstudentform.state }}
{% endif %}
</td>
<td>{{ item.source|default_if_none:"" }}</td>
<td>
{% if item.feature %}
{{ setstudentform.feature|default:item.feature }}
{% else %}
{{ setstudentform.feature }}
{% endif %}
</td>
</tr>
</form>
{% endfor %}
but it's generated html was like this:
<form method="" action=""></form>
<input type="hidden" value="8N1O4Oks4MmgN1ujanMZX0o2X5XGUMny" name="csrfmiddlewaretoken">
with nothing inside the <form>, why is that
You want this:
<form method="POST">
{% csrf_token %}
<table>
{% for item in studentinfo %}
<tr>
<td>...</td>
</tr>
{% endfor %}
</table>
</form>
The way you template is structured is not valid HTML.
Also, you don't need this:
{% if item.feature %}
{{ setstudentform.feature|default:item.feature }}
{% else %}
{{ setstudentform.feature }}
{% endif %}
Simply {{ setstudentform.feature|default:item.feature }} will do what you intend.
The question is incomplete, I think -- we'd need to see what's around that {% for %} too, but I'll hazard a guess.
It looks like you're trying to interleave forms between table rows, which isn't likely legal in HTML and will cause the parsed DOM tree to look possibly something like your "generated HTML" example.
Did you check the HTML on the wire (using View Source (aside from IE, which when I last used it always output the parsed DOM tree as "source"), not a DOM inspector)? My educated guess is you'll find your HTML correct (or well, at least as you've templated it :) ) there.
So, in short, I don't think
<table>
<form><tr>...</tr></form>
<form><tr>...</tr></form>
</table>
isn't legal HTML.