django-class-based-auth-views
Installation
pip install django-class-based-auth-views
Basic usage
Instead of including django.contrib.auth.login into your urls.py, just use the one provided by this project. urls.py:
from class_based_auth_views.views import LoginView, LogoutView
urlpatterns = patterns('',
url(r'^login/$', LoginView.as_view(form_class=EmailAsUsernameAuthenticationForm), name="login"),
url(r'^logout/$', LogoutView.as_view(), name="logout"),
)
Be aware that the logout view requires a POST to actually logout. So the registration/logout.html template should contain a form with a submit button.
Extending LoginView Example
Now that LoginView is based on generic class based views it is much easier to extend. Say you need to implement a 2 step login procedure with a one time password:
from django.contrib.auth import login
class PhaseOneLoginView(LoginView):
def form_valid(self, form):
"""
Forces superusers to login in a 2 step process (One Time Password). Other users are logged in normally
"""
user = form.get_user()
if user.is_superuser:
self.save_user(user)
return HttpResponseRedirect(self.get_phase_two_url())
else:
login(self.request, user)
return HttpResponseRedirect(self.get_success_url())
def get_phase_two_url(self):
return reverse('phase_two_login')
def save_user(self, user):
self.request.session['otp_user'] = user
class PhaseTwoLoginView(FormView):
form_class = OTPTokenForm
def get_user(self):
return self.request.session.get('otp_user', None)
def clean_user(self):
if 'otp_user' in self.request.session:
del self.request.session['otp_user']
def form_valid(self, form):
code = form.cleaned_data.get('code')
user = self.get_user()
login(request, user)
Release files for django-class-based-auth-views 0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-class-based-auth-views-0.4.tar.gz | 5.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_class_based_auth_views-0.4-py2-none-any.whl | Python 2 | none | any | Details |
Total release size: 12.1 kB
Release files / django-class-based-auth-views-0.4.tar.gz
| Download URL | django-class-based-auth-views-0.4.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
52f9fe1576fbb5fff20c26a5f8db8280342e6925644af73fdd0cf37848745269
|
|
BLAKE2b-256 checksum How to use checksums |
ffc008d972d24cbb67f4139deb2b5c2ef02dada60a71b4be477ef687f88c962d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_class_based_auth_views-0.4-py2-none-any.whl
| Download URL | django_class_based_auth_views-0.4-py2-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 2 |
|
SHA-256 checksum How to use checksums |
466b68f5ff345b88b75db1959de3032cfa7f0fab0d5aa0765373f825f692e8f0
|
|
BLAKE2b-256 checksum How to use checksums |
433cf984270c2193161451fbe36b171537f5f3f1fe2e2cef34ffc1c142e9c3ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |