Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble with user.is_authenticated in django template

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)) 
like image 342
Xonal Avatar asked Jun 10 '13 20:06

Xonal


People also ask

How do I authorize a user in Django?

auth import authenticate, login def my_view(request): username = request. POST['username'] password = request. POST['password'] user = authenticate(username=username, password=password) if user is not None: if user.

How do I authenticate login in Django?

In a nutshell, these four commands create a new Django project named src, enter the project, create a new app, mysite, inside the src project, then create a SQLite database for the project named db. sqlite3. Also be sure to include the mysite app inside src/settings.py. INSTALLED_APPS = [ 'src', 'django.

How do I authenticate username and password in Django?

from django. contrib. auth import authenticate user = authenticate(username='john', password='secret') if user is not None: if user. is_active: print "You provided a correct username and password!" else: print "Your account has been disabled!" else: print "Your username and password were incorrect."


1 Answers

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) 
like image 193
Alasdair Avatar answered Sep 20 '22 02:09

Alasdair