Django 1.11+ django.contrib.auth Class Based Views - Part 1 - Login & Logout

This is the first in a long series on creating a CryptoCurrency management site, but first we need to lock things down so people can't see all of our stuff. So we will start by setting up our app and logging in and out. We will do this with the new Class Based Views in django.contrib.auth system.
Download HD Version next video: Django 1.11+ django.contrib.auth Class Based Views - Part 2 - Password Change and Reset
./mananage.py startapp accounts
mkdir -p accounts/templates/accounts

cryptomanager.urls

url(r'^', include('accounts.urls')),

accounts/urls.py

from django.conf.urls import url

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

urlpatterns = [
    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')
]

cryptomanager/urls.py

INSTALLED_APPS = [
    ...
    'accounts',
]

LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'

accounts/templates/accounts.html

{% extends "base.html" %}

{% block content %}
<form action="" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="login" /><br />
    <a href="{% url "register" %}">Register an Account</a>
    <a href="{% url "password_reset" %}">Can't Remember Password</a>
</form>
{% endblock %}
comments powered by Disqus