Skip to main content

Django authentication using class based views

Django 1.11 has introduced Class-Based Views for Authentication. In this post, we'll look over how to use these class based views in our project.

Let's start with the implementation of "Register User" functionality first. There is no pre-defined class based view for this. So, I'll use CreateView for achieving this task. Through out this post, I'll assume that we are using accounts app.

Register User Functionaility : 

Forms.py for user registration (accounts/forms.py)

# Forms.py ( accounts app )
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

class RegisterUserForm(UserCreationForm):
    class Meta:
        model = User
        fields = (
                'username',
                'first_name',
                'last_name',
                'email',
                'password1',
                'password2',
            )

Views.py for user registration (accounts/views.py)

# Views.py ( accounts app )
from django.views.generic import CreateView
from .forms import RegisterUserForm

class RegisterUserView(CreateView):
    form_class = RegisterUserForm
    template_name = 'accounts/register.html'
    success_url = '/'

Template for user registration (accounts/templates/accounts/register.html)

{% extends "base.html" %}
{% block content %}
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
    </form>
{% endblock %}

URL pattern for user registeration

from .views import RegisterUserView
url(r'^register/$', RegisterUserView.as_view(), name='register')

Login, Logout, Password Change & Password Reset :

Functionality of user login, logout, password change and password reset is straight-forward. URL patterns file for accounts app will do most of the work.

from django.conf.urls import url
from .views import RegisterUserView

from django.urls import reverse_lazy
from django.contrib.auth.views import(
    LoginView,
    LogoutView,
    PasswordResetView,
    PasswordResetDoneView,
    PasswordChangeView,
    PasswordChangeDoneView,
    PasswordResetConfirmView,
    PasswordResetCompleteView
)

urlpatterns = [
    url(r'^register/$', RegisterUserView.as_view(), name='register'),
    url(r'^login/$', LoginView.as_view(template_name='accounts/login.html'), name='login'),
    url(r'^logout/$', LogoutView.as_view(next_page=reverse_lazy('login')), name='logout'),
    url(r'^password-change/$', PasswordChangeView.as_view(template_name='accounts/password-change.html'), name='password-change'),
    url(r'^password-change-done/$', PasswordChangeDoneView.as_view(template_name='accounts/password-change-done.html'), name='password_change_done'),
    url(r'^password-reset/$', PasswordResetView.as_view(template_name='accounts/password-reset.html'), name='password-reset'),
    url(r'^password-reset-done/$', PasswordResetDoneView.as_view(template_name='accounts/password-reset-done.html'), name='password_reset_done'),
    url(r'^password-reset-confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', PasswordResetConfirmView.as_view(template_name='accounts/password-reset-confirm.html'), name='password_reset_confirm'),
    url(r'^password-reset-complete/$', PasswordResetCompleteView.as_view(template_name='accounts/password-reset-complete.html'), name='password_reset_complete'),
]

Template for user login (accounts/templates/accounts/login.html)

{% extends "base.html" %}
{% block content %}
    <form method="post" action="{% url 'login' %}>
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
    </form>
{% endblock %}

Template for password change (accounts/templates/accounts/password-change.html)

{% extends "base.html" %}
{% block content %}
    <form method="post" action="{% url 'password-change' %}>
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
    </form>
{% endblock %}

Template for password change (accounts/templates/accounts/password-change-done.html)

{% extends "base.html" %}
{% block content %}
<h2>Password changed successfully!</h2>
{% endblock %}

Template for password reset (accounts/templates/accounts/password-reset.html)

{% extends "base.html" %}
{% block content %}
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
    </form>
{% endblock %}

Template for password reset (accounts/templates/accounts/password-reset-done.html)

{% extends "base.html" %}
{% block content %}
<h2>An email has been sent with the instructions</h2>
{% endblock %}

Template for password reset (accounts/templates/accounts/password-reset-confirm.html)

{% extends "base.html" %}
{% block content %}
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
    </form>
{% endblock %}

Template for password reset (accounts/templates/accounts/password-reset-complete.html)

{% extends "base.html" %}
{% block content %}
<h2>Password Reset Done</h2>
{% endblock %}

Comments