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))
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.
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.
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."
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With