Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

user auth template tags not working-Django

I used django-registration app. Everything goes fine. User gets registered with email verification but when user logs in and is redirected to mainpage the auth template tags such as {% if user.is_authenticated %} returns false.

I am having this in my login.html

<input type="hidden" name="next" value="/" />

After login I want to redirect the user to main page and in mainpage.html

{% if user.is_authenticated %}
     <p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
    <p>Welcome, new user. Please log in.</p>  
{% endif %}

but user.is_authenticated returns false here. What might be the problem here? Thanks

like image 568
Nazim Zeeshan Avatar asked Dec 02 '25 06:12

Nazim Zeeshan


1 Answers

Try using {% if request.user.is_authenticated %} instead. It's entirely possible that user isn't being passed into the context dictionary within the view. If the object is not found within the template, it will just skip to the else portion of the block. Rendering templates are strange in django, as what would normally be an exception, is swallowed.

like image 150
Josh Smeaton Avatar answered Dec 04 '25 19:12

Josh Smeaton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!