if == condition does not output expected results in django template

A beginner here. Apologies if this is a very basic question.

I am working with the Django template using python. My if conditional doesn’t seem to work. Here I assumed a basic string comparison between a and be should yield "Very Happy," the second block. It doesn’t. I output to display in my browser window and the result is always "Very Frustrating." I never get "Very Happy" the 2nd block.

Here is a simplified version of my code:

a = "test4" b = "test5"  {% if a == b %} <strong>Very Frustrating</strong> {% else %} <strong>Very Happy</strong> {% endif %} 

My actual code will have a and b be variable imports (in order to ensure only logged in users can do certain things). Like so: a = {{logged_user}} b = {{clicked_username}}

I put in the simplest test values for a and b (2 different strings) to isolate the issue for clarity. I can provide the actual code. But I figured if this doesn’t work as expected with simple strings as is, this is where the problem is.

Thank you for your time.

Add Comment
2 Answer(s)

Preliminary we can start with checking types of the two variables

 a = {{logged_user}} //(as per naming seems like it would be a User class object)  b = {{clicked_username}} //(as per naming seems like it would be a string) 

Check There data types and if you find so then it has to be a.username getting compared with b

Add Comment

I would check if is there anything wrong with them. Can you put full code with variables assignments ?

Answered on July 15, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.