Django template comparing string - python

I'm new with django. I'm stuck with the problem of comparing string in the template.
I have use ifnotequal tag to compare string. But it is not working.
I have try to output the variable:
{{ request.user.username }}
{{ article.creator }}
Here I compare:
{% ifnotequal request.user.username article.creator %}
{# output something #}
{% endifnotequal %}
But when I do the hardcode: It works.
{% ifnotequal "justin" "mckoy" %}
{# output something #}
{% endifnotequal %}
what is the problem? The article.creator is coming from the database and the user.username is from the request.
Can anyone help me with this issue?

For string compare in template use
{% if name == "someone" %}
............
............
{% endif %}
and for not equal
{% if name != "someone" %}
............
............
{% endif %}

Try this:
{% ifnotequal article.creator|stringformat:"s" request.user.username %}

article.creator is a User and request.user.username is a string. Try comparing request.user instead.

{% ifequal material.unit 'U' %}
<p>are equals!<p/>
{% endifequal %}

Note that if you do not put spaces before and after ==, Django could not parse the expression.
{% if MyProd.Status == "Processing" %}
<button class="btn btn-outline-warning">{{MyProd.Status}}</button>
{% else %}
<button class="btn btn-outline-success">{{MyProd.Status}}</button>
{% endif %}

Related

Using a variable in Django yesno template tag

This could rather be straightforward. However, allow me to ask. In the Django yesno inbuilt template tag, the syntax is given as {{ value|yesno:"yeah,no,maybe" }}.
However, I want to out a variable value if it evaluates to true, something like: {{ request.user.profile.mobile_number|yesno:$variable1, 'string' }}.
I have tried this {{ request.user.profile.mobile_number|yesno:"{{ request.user.profile.mobile_number }},No Contact" }} but I keep on getting an error: Could not parse the remainder: ':"{{ request.user.profile.mobile_number' from 'request.user.profile.mobile_number|yesno:"{{ request.user.profile.mobile_number'
Try this:
{% with yesno_args=request.user.profile.mobile_number|add:", No Contact" %}
{{ request.user.profile.mobile_number|yesno:yesno_args }}
{% endwith %}
Alternatively, which may be more clear:
{% if request.user.profile.mobile_number is not None and request.user.profile.mobile_number != '' %}
<span>{{ request.user.profile.mobile_number }}</span>
{% else %}
<span>No Contact</span>
{% endif %}
Hope this helps!

Jinja loop.index does not print

When running the following jinja code, I only get "Column info" printed. Why the index does not appears ?
{% for field in columns_form %}
{% if 'title_' in field.name %}
<td>Column {{ loop.index }} info</td>
{% endif %}
{% endfor %}
It sounds like the template is being treated as a Django template, not a Jinja template.
Using {{ loop.index }} should work in a Jinja template, but wouldn't work in a Django template, where you would use {{ forloop.counter }} instead.
In case {{ loop.index }} does not work in the latest version, a workaround is to zip the columns_form and range(0, len(columns_form)+1) in the python file as
columns_form_idx = zip(columns_form, range(0, len(columns_form)+1))
In the template file,
{% for field, idx in columns_form_idx %}
{% if 'title_' in field.name %}
<td>Column {{ idx }} info</td>
{% endif %}
{% endfor %}

Next field django forms [duplicate]

I want to put break and continue in my code, but it doesn't work in Django template. How can I use continue and break using Django template for loop. Here is an example:
{% for i in i_range %}
{% for frequency in patient_meds.frequency %}
{% ifequal frequency i %}
<td class="nopad"><input type="checkbox" name="frequency-1" value="{{ i }}" checked/> {{ i }} AM</td>
{{ forloop.parentloop|continue }} ////// It doesn't work
{ continue } ////// It also doesn't work
{% endifequal %}
{% endfor%}
<td class="nopad"><input type="checkbox" name="frequency-1" value="{{ i }}"/> {{ i }} AM</td>
{% endfor %}
Django doesn't support it naturally.
You can implement forloop|continue and forloop|break with custom filters.
http://djangosnippets.org/snippets/2093/
For-loops in Django templates are different from plain Python for-loops, so continue and break will not work in them. See for yourself in the Django docs, there are no break or continue template tags. Given the overall position of Keep-It-Simple-Stupid in Django template syntax, you will probably have to find another way to accomplish what you need.
For most of cases there is no need for custom templatetags, it's easy:
continue:
{% for each in iterable %}
{% if conditions_for_continue %}
<!-- continue -->
{% else %}
... code ..
{% endif %}
{% endfor %}
break use the same idea, but with the wider scope:
{% set stop_loop="" %}
{% for each in iterable %}
{% if stop_loop %}{% else %}
... code ..
under some condition {% set stop_loop="true" %}
... code ..
{% endif %}
{% endfor %}
if you accept iterating more than needed.
If you want a continue/break after certain conditions, I use the following Simple Tag as follows with "Vanilla" Django 3.2.5:
#register.simple_tag
def define(val=None):
return val
Then you can use it as any variable in the template
{% define True as continue %}
{% for u in queryset %}
{% if continue %}
{% if u.status.description == 'Passed' %}
<td>Passed</td>
{% define False as continue %}
{% endif %}
{% endif %}
{% endfor %}
Extremely useful for any type of variable you want to re-use on template without using with statements.

How to output a comma delimited list in jinja python template?

If I have a list of users say ["Sam", "Bob", "Joe"], I want to do something where I can output in my jinja template file:
{% for user in userlist %}
{{ user }}
{% if !loop.last %}
,
{% endif %}
{% endfor %}
I want to make the output template be:
Sam, Bob, Joe
I tried the above code to check if it was on the last iteration of the loop and if not, then don't insert a comma, but it does not work. How do I do this?
You want your if check to be:
{% if not loop.last %}
,
{% endif %}
Note that you can also shorten the code by using If Expression:
{{ ", " if not loop.last else "" }}
You could also use the builtin join filter like this:
{{ users|join(', ') }}
And using the joiner from https://jinja.palletsprojects.com/templates/#joiner
{% set comma = joiner(",") %}
{% for user in userlist %}
{{ comma() }}{{ user }}
{% endfor %}
It's made for this exact purpose. Normally a join or a check of forloop.last would suffice for a single list, but for multiple groups of things it's useful.
A more complex example on why you would use it.
{% set pipe = joiner("|") %}
{% if categories %} {{ pipe() }}
Categories: {{ categories|join(", ") }}
{% endif %}
{% if author %} {{ pipe() }}
Author: {{ author() }}
{% endif %}
{% if can_edit %} {{ pipe() }}
Edit
{% endif %}

'for' loop through form fields and excluding one of the fields with 'if'

The problem I'm struggling with is as follows:
I have:
{% for field in form %}
{{ field }}
{% end for %}
What I want is to put an 'if' statement to exclude a field which .label or whatever is provided. Like:
{% for field in form%}
{% if field == title %}
{% else %}
{{ field }}
{% endif %}
{% endfor %}
Is it possible? I have to many fields to write them one by one and only one or two to exclude.
Thank you for any tips.
BR,
Czlowiekwidmo.
Yes, this should be possible:
{% for field in form %}
{% ifnotequal field.label title %}
{{ field }}
{% endifnotequal %}
{% endfor %}
Django's template tags offer ifequal and ifnotequal variants, and you can test the field.label against either a context variable, or a string.
You might be a lot happier creating a subclass of the form, excluding the offending field.
See http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#form-inheritance
class SmallerForm( MyForm ):
class Meta(MyForm.Meta):
exclude = [ title ]
{% for field in form %}
{% if field.name != 'field_name' %}
{{ field }}
{% endif %}
{% endfor %}

Categories