Django: Issues with if condition in templates - python

I want to print something on the basis of the current language code. For that I did something like this:
{% if request.LANGUAGE_CODE == en %}
<h1>English</h1>
{% endif %}
But this if condition does not compare the current language code. But if I print this {{request.LANGUAGE_CODE}} on the same page then it will print en as language code, but my if condition is not working and I don't know why ??

LANGUAGE_CODE is a string, so you just need to enquote your comparison value like this:
{% if request.LANGUAGE_CODE == 'en' %}
<h1>English</h1>
{% endif %}
check also the ifequal tag
{% ifequal request.LANGUAGE_CODE 'en' %}
...
{% endifequal %}
a bit more: the if and ifequal on strings are case sensitive, so you may want to be sure you're matching the correct case (maybe applying the |lower filter to both arguments)

Related

django janja compare database value

I have some data store in the database In boolean. I want to check if the value is True pick the value and perform some action on it.
product: "{{DB_report_query.product.name}}"
{% if DB_report_query.product.summary == True %}
{{DB_report_query.summary}}
{% endif %}
But this does not work.
If it is a BooleanField then simply use the following:
{% if DB_report_query.product.summary %}
{{DB_report_query.summary}}
{% endif %}

How to check if a dict's member is equal to a string in Jinja2

I have a Jinja2 Template that I am serving in flask that looks somthing like this:
{% if current_user.UserType == “Admin” %}
Stuff
{% endif %}
However I am getting an error like this
TemplateSyntaxError: unexpected char u'\u201c' at 860
What is the proper way to check a key's value in Jinja2?
It looks like the problem was the one downshift mentioned with encoding, I was under the impression that it was somthing to do with the jinja2 syntax but,
{% if current_user.UserType == "Admin" %}
Stuff
{% endif %}
works just fine.
Use bracket notation:
{% if current_user["UserType"] == “Admin” %}
Stuff
{% endif %}
Or the get method:
{% if current_user.get("UserType") == “Admin” %}
Stuff
{% endif %}
Also use Google

Modify django templates to check for values in a dict

I have the following Django template.
{% load custom_tags %}
<ul>
{% for key, value in value.items %}
<li> {{ key }}: {{ value }}</li>
{% endfor %}
I need to check for the value and do some modifications.
If the value is True , instead of value I have to print Applied , else if it False I need to print Not Applied.
How to achieve that?
Very simple if-else clause here. Take a look at the django template docs to familiarize yourself with some of the common tags.
{% if value %}
APPLIED
{% else %}
NOT APPLIED
{% endif %}
You asked how to do this as a filter... I'm not sure why, but here is it:
In your app's templatetags directory create a file called my_tags.py or something and make the contents
from django import template
register = template.Library()
#register.filter
def applied(value):
if value:
return 'Applied'
else:
return 'Not applied'
Then in your template make sure to have {% load my_tags %} and use the filter with {{ value|applied }}

Django TemplateSyntaxError in with template tag

I'm trying to use this app in my project.
https://github.com/streema/django-favit
I already can use the fav-unfav part of this app. I also want to list favourites of user for every user. In read me part it says use this and it will be listed but I have an error with
{% with user_favorites <user> "baslik.Entry" as favorite_list %}
{% for fav_obj in favorite_list %}
{{ fav_obj }}
{% endfor %}
{% endwith %}
Error:
TemplateSyntaxError at /
u'with' expected at least one variable assignment
This is the template tag part for user_favorites:
#register.assignment_tag
def user_favorites(user, app_model=None):
"""
Usage:
Get all user favorited objects:
{% with user_favorites <user> as favorite_list %}
{% for fav_obj in favorite_list %}
{# do something with fav_obj #}
{% endfor %}
{% endwith %}
or, just favorites from one model:
{% with user_favorites <user> "app_label.model" as favorite_list %}
{% for fav_obj in favorite_list %}
{# do something with fav_obj #}
{%
{% endwith %}
"""
return Favorite.objects.for_user(user, app_model)
How can I get rid of this error? Thanks.
It's a reasonably common convention in documentation that anything in angle brackets is a placeholder to be replaced by the actual value. In this case, <user> is supposed to be replaced by the object containing the actual user.
{% with user_favorites request.user ...
I must say, though, that the documentation still doesn't make any sense. You can't use an assignment tag in a with statement like that - even after correcting the user issue, this still won't work. The confusing thing is that the same syntax is repeated throughout the documentation, but it simply doesn't work.
I think this is simply a bug with the documentation, and suspect that if you simply remove the word "with" this will work.
To use custom template tag in django, it is needed to explicitly load it in template.
Add this line at the beginnig of your template (but after {% extends ... %}, if you have such):
{% load favit_tags %}
Looks like this step is missed from django-favit README.

Django if statement doesn't work as expected

I have the following in my html file:
{% trans "Result: "%} {{result}}
Which will print out the word SUCCESS on the browser (because thats what the string contains)
But If I do the following:
{% if result == 'SUCCESS' %}
do something
{% else %}
do something else
{% endif %}
I find that the if statement does not work as expected.
Why is this??
The if statement works fine. Your problem must be regarding the string. Maybe it's not a string at all.
Try the ifequal templatetag:
{% ifequal result 'SUCCESS' %}
do something
{% else %}
do something else
{% endifequal %}
You can try different things. If you're assigning result in a view, you can validate it's a string in that very same view:
def my_view(request):
# ... processing ...
result = something()
# Let's make sure it's a string containing 'SUCCESS'
assert type(result) == str
assert result == 'SUCCESS'
You can apply the same logic if it's a context processor.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#ifequal
Check this link:
Django String format.
according to django documentation you should use this format:
{% if result|stringformat:"s" == 'SUCCESS' %}
do something
{% else %}
do something else
{% endif %}
or
{% if result|stringformat:"s" in 'SUCCESS' %}
do something
{% else %}
do something else
{% endif %}
or
{% ifequal result|stringformat:"s" 'SUCCESS' %}
do something
{% else %}
do something else
{% endif %}
this problem happen because of your variable type, you should change it to string before compare it to another string.

Categories

Resources