Django — default (built-in) user authentication system
By: Mihai Corciu
Tags: Authentication, Django

Initializing and starting python virtual environment:

python -m venv env source env/bin/activate

Installing python packages:

pip install Django

Creating project: instead of known command

django-admin startproject default_auth

which generates the structure:

default_auth/

├── default_auth

│ ├── asgi.py

│ ├── __init__.py

│ ├── settings.py

│ ├── urls.py

│ └── wsgi.py

└── manage.py

we could use, alternatively:

mkdir default_auth

cd default_auth

django-admin startproject src .

which generates structure:

default_auth

├── manage.py

└── src

├── asgi.py

├── __init__.py

├── settings.py

├── urls.py

└── wsgi.py

Django, automatically installed ‘django.contrib.auth’ modules in /src/settings.py which will perform all the ‘magick’ af user authentication.

INSTALLED_APPS = [

'django.contrib.admin',

'django.contrib.auth', #here

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

]

To use ‘auth’ we need to activate predefined urls of this package in /src/urls.py

from django.contrib import admin

from django.urls import path, include #add this urlpatterns = [

path('admin/', admin.site.urls),

path('accounts/', include('django.contrib.auth.urls')), #add this

]

After:

python manage.py migrate

python manage.py runserver

and accesing

django gives as a ‘Page not found (404)’ in which we could see listed the urls provided by ‘auth’ module:

admin/ accounts/ login/ [name=’login’] accounts/ logout/ [name=’logout’] accounts/ password_change/ [name=’password_change’] accounts/ password_change/done/ [name=’password_change_done’] accounts/ password_reset/ [name=’password_reset’] accounts/ password_reset/done/ [name=’password_reset_done’] accounts/ reset/<uidb64>/<token>/ [name=’password_reset_confirm’] accounts/ reset/done/ [name=’password_reset_complete’]

Django ‘auth’ application has built-in url and views for login and logout so, all we need is to create the templates for login and logout procedures:

mkdir templates

mkdir templates/registration

Update /src/settings.py

TEMPLATES = [

{

...

'DIRS': [str(BASE_DIR.joinpath('templates'))],

...

},

]

Create ‘/templates/base.html’ (using Bootstrap 4 starter template):

Login and logout

We need to create login and logout templates.

First, create an accounts app

python manage.py startapp accounts

Update /src/urls.py

urlpatterns = [

path('admin/', admin.site.urls),

path('accounts/', include('django.contrib.auth.urls')),

path('', include('accounts.urls')), #add this

]

Create /accounts/urls.py

from django.urls import path

from . import views urlpatterns = [

path('', views.home, name = 'home'),

]

Now, we are able to access home page at ‘http://localhost:8000/’ which instructs us to login.

Designing a login template:

/templates/registration/login.html

After login we need a home redirect home page.

‘/accounts/views.py’

from django.shortcuts import render def home(request):

return render(request, 'home.html', {})

Add to ‘/src/settings.py’ to redirect to home.html page after login:

LOGIN_REDIRECT_URL = '/'

create ‘/templates/home.html’

As we don’t have any user, for simplicity, we coukd use a superuser first which we define with:

python manage.py createsuperuser

or, we can create users in admin inteface.

After logout we are redirected to an ugly admin template so we also need a redirect to home.html instruction in /src/settings.py

LOGOUT_REDIRECT_URL = '/'

Now, we have a login/logout authentication system working. We need some design improvement for the login template with Django Crispy Forms:

pip install django-crispy-forms

Add ‘crispy_forms’ to INSTALLED_APPS in ‘/src/settings’.

Update’ /templates/registration/login.html’

Now, username and password fields from login.html form have equal width.

Signup

For a signup page we need to create our own view and url.

Add ‘accounts’ to INSTALLED_APPS from ‘/src/settings’

Update ‘/accounts/urls.py’

from django.urls import path

from . import views urlpatterns = [

path('', views.home, name = 'home'),

path('accounts/signup/', views.signup, name='signup'), #add this

]

Update ‘/accounts/views.py’ with:

create ’/templates/registration/signup.html’

Password reset

First of all we need to activate the Django development mail service (Django testing SMTP service) and the testing directory where the reset email password will be deployed. Add this in /src/settings.y

#fake mail deliver

EMAIL_BACKEND = "django.core.mail.backends.filebased.EmailBackend"

EMAIL_FILE_PATH = str(BASE_DIR.joinpath('sent_emails'))

The default reset templates are not very pretty so ve need to redesign them.

Cretate ‘/templates/registration/password_reset_form.html’

‘/templates/registration/password_reset_done.html’

‘templates/registration/password_reset_complete.html’

Finnaly, update /templates/home.html

When accesin ‘http://localhost:8000/accounts/password_reset/’, be sure that there are users that have emails defined. If the mail entered does not exist as a mail of a user, there will be no answer.

That’s all.

Resources: