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 use开发者_如何学Pythonr. Please log in.</p>
{% endif %}
but user.is_authenticated returns false here. What might be the problem here? Thanks
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.
Can you show us the code for the view which handles the URL '/'? I think this is where your problem lies, rather than in your use of django-registration.
Does this view put user
in the context? If you want to use it in the template, you're going to have to put it in. If you want to use request
in the context, then you need to make sure that you pass an instance of RequestContext
as the context rather than just a standard context object.
Here's what worked for me.
Suppose your main page
gets processed by a view in views.py
from django.shortcuts import render_to_response
def mainpage(request):
return render_to_response('mainpage.html')
You need to add RequestContext to include to user object. Change your view to
from django.shortcuts import render_to_response
from django.template import RequestContext
def mainpage(request):
return render_to_response('mainpage.html', context_instance=RequestContext(request))
精彩评论