I want to define a variable in my Django template but if I set the variable within the statement I can't call it again. Is there any way to call the variable from the other part of my template? Thank you in advance!
My attemt:
{% if a > 1 %}
{% with "string_1" as var %}
{% endwith %}
{% elif a < 1%}
{% with "string_2" as var %}
{% endwith %}
{% else %}
{% with "string_3" as var %}
{% endwith %}
{% endif %}
{% if var='string_1' %} #here i want to call the variable again
<p>This is string_1</p>
{%else%}
<p>This is not string_1</p>
Related
I'd like to declare in the template a variable that allows me to position an image inside a section on the left or the right. I just don't understand how can I do it on the variable side.
Here's my current code,
main.html :
{% with img_left=False %}
{% include 'section-service.html' %}
{% endwith %}
section-service.html:
<div class="sect_service-inside {% if img_left=True %} serv_img-left {% else %} serv_img-right {% endif %}">
<div id="here_img">...</div>
<div id="here_text">...</div>
</div>
but I get the error Could not parse the remainder: '=True' from 'img_left=True'
Note that I'll use this section multiple times in the main.html page, that's why I'd like to have the variable left-right.
EDIT:
views.py:
def page(request):
return render(request, 'main.html')
I suppose you want widget-tweaks
As example for form:
{% for field in form %}
{% if <something> %}
{% render_field field class="<first class>" %}
{% elif <something other> %}
{% render_field field class="<second class>" %}
{% else %}
{% render_field field class="<other class>" %}
{% endif %}
{% endfor %}
I have a Django multiple 'with' statements inside 'if/elif' statement.
The blocks of code inside if/elif are the same except for one variable 'slide1_line2'.
I am wondering if there is a way to re-write code to avoid repetition.
{% if country == 'England' or country == 'Wales'%}
{% with graphic='map.png' %}
{% with classes='color-1' %}
{% with slide1_line1='Your constituency is '|add:name %}
{% with slide1_line2='Heading1' %}
{% with slide1_line3='text' %}
{% with icon='keyboard_arrow_down' %}
{% include 'slide_map.html' %}
{% endwith %}{% endwith %}{% endwith %}{% endwith %}{% endwith %}{% endwith %}
{% elif country == 'Scotland' or country == 'Northern Ireland' %}
{% with graphic='map.png' %}
{% with classes='color-1' %}
{% with slide1_line1='Your constituency is '|add:name %}
{% with slide1_line2='Heading2' %}
{% with slide1_line3='text' %}
{% with icon='keyboard_arrow_down' %}
{% include 'slide_map.html' %}
{% endwith %}{% endwith %}{% endwith %}{% endwith %}{% endwith %}{% endwith %}
{% endif %}
You can factor out the common values outside the if statement:
{% with graphic='map.png' classes='color-1' slide1_line1='Your constituency is '|add:name slide1_line3='text' icon='keyboard_arrow_down' %}
{% if country == 'England' or country == 'Wales' %}
{% include 'slide_map.html' with slide1_line2='Heading1' %}
{% else %}
{% include 'slide_map.html' with slide1_line2='Heading2' %}
{% endif %}
{% endwith %}
Note that:
you can define multiple variables in the same {% with … %} template tag [Django-doc]; and
the {% include … %} template tag [Django-doc] can have a {% include … with … %} clause to pass certain extra parameters.
I want to do assignment in Django template tag for this code:
{% for ins in ob %}
{% if ins.heur = 'here' %}
a==1
some stuff ..
{% endif %}
{% endfor %}
{% if a!=1 %}
stuff
{% endif %}
You can use the {% with %} template tag.
Example:
{% with a=1 %}
{{ a }}
{% endwith %}
I'm doing this in one of my templates:
{% if activite.travel %}
{% with activite.travel.personne as personne %}
{% include 'includes/person_detail.html' %}
{% endwith %}
{% endif %}
{% if activite.relation %}
{% with activite.relation.src as personne %}
{% include 'includes/person_detail.html' %}
{% endwith %}
{% endif %}
Note: I may have more fields to come in activite that's why I'm not doing "else" but two separate "if".
I'd like to do something like:
{% if activite.travel %}
{% set personne=activite.travel.personne %}
{% elsif activite.relation %}
{% set personne=activite.relation.src %}
{% endif %}
{% include 'includes/person_detail.html' %}
Is there a way to do this in the template?
Not exactly... but you can use with inside of include
{% if activite.travel %}
{% include 'includes/person_detail.html' with personne=activite.travel.personne %}
{% elif activite.relation %}
{% include 'includes/person_detail.html' with personne=activite.relation.src %}
{% endif %}
I have the following loop set up, but need to remove the comma on the last item (it's to replicate a JSON array for cycle2)
{% for product_in_series in series.get_products %}{%spaceless%}
{% with product_in_series.product as product %}
{%if not forloop.first%}
"<img src='{% version product.get_overview 'page_image' %}'>",
{%endif%}
{% endwith %}
{%endspaceless%}{% endfor %}
Cheers,
R
What about this?
{% for product_in_series in series.get_products %}{%spaceless%}
{% with product_in_series.product as product %}
{%if not forloop.first%}
"<img src='{% version product.get_overview 'page_image' %}'>"
{%if not forloop.last%},{%endif%}
{%endif%}
{% endwith %}
{%endspaceless%}{% endfor %}
{{ forloop.last|yesno:",,"|safe }}
, - is a comma
None of the above works for me.
The correct syntax, as in Django 3.0, is as such
{% with querythisandthat as A %}
{% for u in A %} {{ u.interesting_stuff }}
{% if u == A.last %} . {% else %} ; {% endif %}
{% endfor %}
{% endwith %}
The reason is that A.last is not True/False, but it is the last element of the queryset
https://docs.djangoproject.com/en/3.0/ref/models/querysets/#django.db.models.query.QuerySet.first