How to override Django-Allauth default templates

The problem

I chose the package ‘django-allauth’ to help me with the login management of a SaaS code base that I am building. All my installed packages are inside of my virtual environment folder (venv) inside my project folder.

I had already created a base layout for the landing page. However, after installing the ‘django-allauth’ and configuring it, I noticed that the login page did not inherited the layout configuration from my base template.

Figure 1 – Login screen did not inherited layout configuration

The solution

After some research I discovered that I needed to override the default templates. They are stored iwithin the installed library. It is not advised to modify the original templates. Instead, I copied the templates from ‘/venv/lib/python/site-packages/allauth/templates’ to the root folder of my project.

Figure 2 – Copy templates folder

The template folder contains various elements that can be customized to change their appearance across all instances where they are used. The library developers designed this structure to isolate elements from the CSS as much as possible. This approach allows us to modify the CSS styles for each element, affecting all pages uniformly.

To achieve my goal, I modified the ‘templates/layouts/entrance.html’ file by changing the path on the first line to point to my base template:

{% extends "PATH_OF_MY_BASE_TEMPLATE" %}

{% block content %}
{% endblock content %}

Subsequently, I updated the  ‘settings.py’ file to include the directory containing the templates.

Figure 3 – Including templates folder path in the settings.py

After these adjustments, the login screen successfully inherited the base layout.

Figure 4 – Login screen with base layout

Sources

Leave a Reply

Your email address will not be published. Required fields are marked *