Skip to content

Instantly share code, notes, and snippets.

@stefanfoulis
Created August 11, 2011 16:44
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save stefanfoulis/1140136 to your computer and use it in GitHub Desktop.
Save stefanfoulis/1140136 to your computer and use it in GitHub Desktop.
django: class based authentication view (login)
#-*- coding: utf-8 -*-
import urlparse
from django.contrib.auth import REDIRECT_FIELD_NAME, login
from django.contrib.auth.forms import AuthenticationForm
from django.http import HttpResponseRedirect
from django.utils.decorators import method_decorator
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_protect
from django.views.generic.edit import FormView
from django.conf import settings
class LoginView(FormView):
"""
This is a class based version of django.contrib.auth.views.login.
Usage:
in urls.py:
url(r'^login/$',
AuthenticationView.as_view(
form_class=MyCustomAuthFormClass,
success_url='/my/custom/success/url/),
name="login"),
"""
form_class = AuthenticationForm
redirect_field_name = REDIRECT_FIELD_NAME
template_name = 'registration/login.html'
@method_decorator(csrf_protect)
@method_decorator(never_cache)
def dispatch(self, *args, **kwargs):
return super(LoginView, self).dispatch(*args, **kwargs)
def form_valid(self, form):
"""
The user has provided valid credentials (this was checked in AuthenticationForm.is_valid()). So now we
can log him in.
"""
login(self.request, form.get_user())
return HttpResponseRedirect(self.get_success_url())
def get_success_url(self):
if self.success_url:
redirect_to = self.success_url
else:
redirect_to = self.request.REQUEST.get(self.redirect_field_name, '')
netloc = urlparse.urlparse(redirect_to)[1]
if not redirect_to:
redirect_to = settings.LOGIN_REDIRECT_URL
# Security check -- don't allow redirection to a different host.
elif netloc and netloc != self.request.get_host():
redirect_to = settings.LOGIN_REDIRECT_URL
return redirect_to
def set_test_cookie(self):
self.request.session.set_test_cookie()
def check_and_delete_test_cookie(self):
if self.request.session.test_cookie_worked():
self.request.session.delete_test_cookie()
return True
return False
def get(self, request, *args, **kwargs):
"""
Same as django.views.generic.edit.ProcessFormView.get(), but adds test cookie stuff
"""
self.set_test_cookie()
return super(LoginView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
"""
Same as django.views.generic.edit.ProcessFormView.post(), but adds test cookie stuff
"""
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
self.check_and_delete_test_cookie()
return self.form_valid(form)
else:
self.set_test_cookie()
return self.form_invalid(form)
@sajid1189
Copy link

Really helpful.. Could you please include the AuthenticationForm as well so that we can look into the is_valid() method

@nimch
Copy link

nimch commented Apr 1, 2016

@sajid1189 : AuthenticationForm is part of Django framework so you can find it into source... (it calls authenticate !)

@ragnarok22
Copy link

how I can show an error message if the user or password are wrong or the user is inactive?

@banagale
Copy link

banagale commented Jun 1, 2018

This was useful to me, seven years later. Thank you for sharing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment