Reputation: 1350
Sorry if you tried helping me when I asked this earlier. Had to delete that question because I wasn't being allowed to edit additional information for some reason.
I'm working on implementing user authentication on my django website. Everything works. My views, models, urls, etc. are all set up. Users can register, log in, log out. The issue I'm having is with this bit of code:
{% if request.user.is_authenticated %}
<li><a href="/logout">Log Out</a></li>
{% else %}
<li><a href="/login">Log In</a></li>
{% endif %}
Even when I'm logged in, it is still displaying "Log In" as an option rather than "Log Out". However, if I click on the link, it'll redirect me to /profile because that's what the view tells it to do if I'm logged in. So, clearly it knows I'm logged in, but the template isn't readint user.is_authenticated as true.
The view relating to login requests is:
def LoginRequest(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
profile = authenticate(username=username, password=password)
if profile is not None:
login(request, profile)
return HttpResponseRedirect('/profile/')
else:
return render_to_response('template/login.html', {'form': form}, context_instance=RequestContext(request))
else:
return render_to_response('template/login.html', {'form': form}, context_instance=RequestContext(request))
else:
''' user is not submitting the form, show them login form '''
form = LoginForm()
context = {'form': form}
return render_to_response('template/login.html', context, context_instance = RequestContext(request))
Upvotes: 38
Views: 50363
Reputation: 308769
If the auth context processor is enabled, then user
is already in the template context, and you can do:
{% if user.is_authenticated %}
If you want to access request
in the template, make sure you have enabled the request context processor.
In your question you are using render_to_response
. Ever since Django 1.3, it has been better to use render
instead of render_to_response
. Using render_to_response
with RequestContext(request)
works in Django <= 1.9, but from Django 1.10 onwards you must use the render
shortcut if you want the context processors to work.
return render(request, 'template/login.html', context)
Upvotes: 62
Reputation: 23282
Be aware that since Django 1.10 the is_authenticated
is decorated with @property and it behaviour differs.
For UNAUTHENTICATED user calling {{user.is_authenticated}} results:
CallableBool(True)
(when on Django < 1.10 it was True
)
For AUTHENTICATED user calling {{user.is_authenticated}} results:
CallableBool(False)
(when on Django < 1.10 it was False
)
If you need to pass e.g to your javascript value like true
or false
you can do it with applying filter |yesno:"true,false"
<script language="javascript">
var DJANGO_USER = "{{user.is_authenticated|yesno:"true,false"}}";
</script>
Upvotes: 7