Following is the code where I need to update a variable.
{% with a=1 %}
{% for x in object_list %}
{% if 'clone' in x.game and a is 1 %}
<div class="alert alert-primary">
<h2 class="p-3">{{ x.title }}</h2>
</div>
{{ a=2 }} # here is where I update it
{% endif %}
{% endfor %}
{% endwith %}
but instead of {{ a=2 }} setting a to 2, it throws the following error:
TemplateSyntaxError at /
Could not parse the remainder: '=2' from 'a=2'
There is no way to reassign a variable in a with template tag.
You should do that in the backend (view) or by loading the variable into JavaScript and perform what you want to do it it can be client-side.
Like everyone is saying, you can do this logic in the view. But, instead of reassigning a to 2, you can just add 1:
{% with a=1 %}
{% for x in object_list %}
{% if 'clone' in x.game and a is 1 %}
<div class="alert alert-primary">
<h2 class="p-3">{{ x.title }}</h2>
</div>
{{ a|add:"1" }} # this is the changed code
{% endif %}
{% endfor %}
{% endwith %}
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 %}
Im trying to get a bio info for each profile, when running server and going to accounts/profile y find this error:
Could not parse some characters: |% with website=profile.website| | default:"" %
{% extends 'base.html' %}
{% block title %}Profile{% endblock %}
{% block content %}
<h1>
{{ user.get_null_name }} (#{{ user.username }})
</h1>
{% with profile=user.profile %}
{% if profile %}
<h2>
{{ profile.persona }}
</h2>
<div>
{{ profile.bio|default:"" }}
</div>
<div>
{{ % with website=profile.website | default:"" % }}
{{website}}
{% endwith %}
</div>
<br/>
<div>
Interest:
{% for Interest in profile.interest.all %}
<span>
{{ interest.name }}{% if not forloop.last %}, {% endif %}
</span>
{% endfor %}
</div>
{% endif %}
{% endwith %}
{% endblock %}
A template tag does not use double curly brackets ({{ … }}), but single ones, with the % immediately followed by that (so {% … %}). The {% with … %} block in your template uses double curly brackets:
{% with website=profile.website|default:'' %}
…
{% endwith %}
I am using a formset. formset.non_form_errors are displayed exactly the way I need it. However my field specific errors in for dict in formset.errors are always displayed inlcuding the <li> tag. I tried .as_text, however that just added a * (and removed the li). But I don't need the * either.
Anyone who can help me with that?
{% if formset.total_error_count %}
{% if formset.non_form_errors %}
{% for error in formset.non_form_errors %}
<div class="alert alert-warning" role="alert">
{{ error|escape }}
</div>
{% endfor %}
{% endif %}
{% for dict in formset.errors %}
{% for error in dict.values %}
<div class="alert alert-warning" role="alert">
{{ error|escape }}
</div>
{% endfor %}
{% endfor %}
{% endif %}
<form method="post">
{% csrf_token %}
{{ formset.management_form }}
The problem could be fixed by moving the individual errors in the form loop:
{% if formset.total_error_count %}
{% if formset.non_form_errors %}
{% for error in formset.non_form_errors %}
<div class="alert alert-warning" role="alert">
{{ error|escape }}
</div>
{% endfor %}
{% endif %}
{% endif %}
{% for form in formset %}
{% if form.quantity.errors %}
<div class="alert alert-warning" role="alert">
{% for error in form.quantity.errors %}
{{ error|escape }}
{% endfor %}
</div>
{% 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
I would like to modify an admin template in Django.
% cat /Library/Python/2.5/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html
<fieldset class="module aligned {{ fieldset.classes }}">
{% if fieldset.name %}<h2>{{ fieldset.name }}</h2>{% endif %}
{% if fieldset.description %}<div class="description">{{ fieldset.description|safe }}</div>{% endif %}
{% for line in fieldset %}
<div class="form-row{% if line.errors %} errors{% endif %} {% for field in line %}{{ field.field.name }} {% endfor %} ">
{{ line.errors }}
{% for field in line %}
<div{% if not line.fields|length_is:"1" %} class="field-box"{% endif %}>
{% if field.is_checkbox %}
{{ field.field }}{{ field.label_tag }}
{% else %}
{{ field.label_tag }}{{ field.field }}
{% endif %}
{% if field.field.field.help_text %}<p class="help">{{ field.field.field.help_text|safe }}</p>{% endif %}
</div>
{% endfor %}
</div>
{% endfor %}
</fieldset>
What kind of object is field, and more specifically how would I get the name of a field?
field is an instance of AdminField and field.field is an instance of BoundField, so you can reference the fields name with:
{{ field.field.name }}
Once as you start to dive deep into admin customisation, its the only place the documentation is really lacking. that being said, the code is well written and easy to understand if you take the time to research it, IMHO.
There is not many files so take an evening and read through them. In your case, I would start with:
contrib/admin/sites.py
contrib/admin/options.py
contrib/admin/helpers.py
Have you done your research?
After that, I would start poring over the python code that invokes your template. I would imagine that field is from the forms system
Field
A class that is responsible for doing validation, e.g. an EmailField
that makes sure its data is a valid
e-mail address.