1

How can I compare context variable in Django? I want to compare the name that I have saved in my model with request.user

{% if user %}
  {% for x in user %}
    {% ifequal x request.user %}
      <h1>working</h1>
    {% else %}
      <h1>false</h1>
    {% endifequal %}
  {% endfor %}
{% endif %}

I want the following comparison: {{x}} == {{request.user}}

Context dict

{"user": Prod.objects.filter().values_list('name', flat=True)}

User is here for example admin and request.user is also admin

My model:

class Prod(models.Model):
    description = models.CharField(max_length=300)
    price = models.DecimalField(max_digits=6, decimal_places=2)
    name = models.ForeignKey(User, on_delete=models.CASCADE)

Loop

I am using a context processor in the base.html

from farmer_page.models import Prod
def context_variable(request):
    # .values_list('name', flat=True)
    return {"user": Prod.objects.filter()}
3
  • Does this answer your question? Nested 'If' statement in jinja 'for' loop Commented Nov 13, 2019 at 12:51
  • Does Prod table has data related to current logged in user if not {{ request.user.user_name }} will print nothing. Commented Nov 13, 2019 at 13:53
  • request.user.username worked for me now. Commented Nov 13, 2019 at 13:54

2 Answers 2

2

Use if template tag in your templates:

{% if x == request.user %}

Edit

{% if user %}
  {% for x in user %}
    {% if x.name == request.user %}
      <h1>working</h1>
    {% else %}
      <h1>false</h1>
    {% endif %}
  {% endfor %}
{% endif %}
Sign up to request clarification or add additional context in comments.

13 Comments

Prod contains multiple names like admin user1 user2 user3 so I still need to loop through it?
Based on what you want to accomplish, you can either loop through it or use in template tag.
{% if user in request.user %} <h1>working</h1> {% else %} <h2>false</h2> {% endif %} This is not working I get false
If you are looking for request.user in user: {% if request.user in user %}
Still the same result false
|
0

Context dict:

{"user": Prod.objects.filter(condition)}

template

 {% if request.user in user %}
<h1>success</h1>
{% else %}
<h1>failed</h1>
{% endif %}

---------Edit----------

Always use related name in Foreign key

class Prod(models.Model):
    .
    .
    name = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_name')

views.py

{"user": Prod.objects.filter(condition)}

html template

{% if request.user.user_name in user %}
    <h1>success</h1>
    {% else %}
    <h1>failed</h1>
    {% endif %}

11 Comments

Yes I'm doing this but is it not working because user is a queryset?
@GigaMeta what is model "Prod". Is this for users?
Prod stands for Products, each "Prod" has a name and I filter them
@GigaMeta Then this will not work. request.user is current logged in user. It will not match with instance of Product model.
@GigaMeta Is anything common in Prod model and User model?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.