Hi I’m writing a django project, and I write template code like this:
<ul id="nav">
<li><a href="/">Home</a></li>
<li><a href="/user/">Users</a></li>
{% if user %}
<li><a href="/user/{{ user.id }}/">Settings</a></li>
<li><a href="/logout/">Log Out</a></li>
{% else %}
<li><a href="/login/">Log In</a></li>
<li><a href="/signup/">Sign Up</a></li>
{% endif %}
</ul>
Now in login view I write like this:
def login(request):
if user_logged_in(request):
return redirect('/')
if request.method == 'GET':
form = LogInForm()
return render_to_response(LOGIN_PATH, {'form':form}, context_instance=RequestContext(request))
But when I run the server, no user is logged in, and visit login page, it shows Settings and Log Out(there is a user object in context), but it shouldn’t!
If I remove RequestContext, say return render_to_response(LOGIN_PATH, {‘form’:form}), it will be OK. And
return render_to_response(LOGIN_PATH, {'form':form, 'user':None}, context_instance=RequestContext(request))
is OK too. But I don’t want to do it.
I know it’s dirty design, well… I’m looking for suggestions and solutions. Many thanks~!
your tag just checks for a user object, not for an authenticated one.
check here for more informations on what you can do with an auth user 🙂