Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django template comparing string

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?

like image 886
justin Avatar asked Oct 08 '22 18:10

justin


2 Answers

For string compare in template use

{% if name == "someone" %}
   ............
   ............
{% endif %}

and for not equal

{% if name != "someone" %}
   ............
   ............
{% endif %}
like image 71
Nauman Tariq Avatar answered Oct 10 '22 07:10

Nauman Tariq


Try this:

{% ifnotequal article.creator|stringformat:"s" request.user.username %}
like image 66
sandeep sangwan Avatar answered Oct 10 '22 06:10

sandeep sangwan