Skip to content Skip to sidebar Skip to footer

Django + Html: Why Does My If Condition In My Template Fail Even Thought I Havent Submitted Interest?

Django + HTML: Why does my if condition in my template fail even thought I havent submitted interest? As you can see from my views.py i have already indicated that if you have sub

Solution 1:

The issue might be with your if condition in the view

if blog_post.author != request.user:
    submittedinterest = Falseif blog_post.author.interestsender.filter(id=request.user.id).exists():
        submittedinterest = True
    context['submittedinterest'] = submittedinterest
#when the post belongs to meelse:
    pass  //this statement

In the outer if condition, you are not setting any value to the context['submittedinterest'] which means it will not exist in the context. Hence in the template when you do {% if submittedinterest %} it will always evaluate to false if the blog post belongs to you. To fix this you need to do context['submittedinterest'] = False in the outer else condition. The question isn't very clear regarding where exactly the error is occuring, and I cannot comment so I clarified what I think the error could be. If you can clarify more!

Post a Comment for "Django + Html: Why Does My If Condition In My Template Fail Even Thought I Havent Submitted Interest?"