| 1 |
Templates |
| 2 |
========= |
| 3 |
|
| 4 |
Introduction |
| 5 |
------------ |
| 6 |
|
| 7 |
The templates that are offered out of the box are intentionally plain and |
| 8 |
without any styling. We do not want to pick a side in the multitudes of frontend |
| 9 |
styling options out there, and the look and feel typically should be adjusted to |
| 10 |
match the branding of your project. Therefore it is recommended that you copy all |
| 11 |
templates over to your own project and adjust them as you see fit. |
| 12 |
|
| 13 |
Having said that, with features such as third party account providers |
| 14 |
and two-factor authentication, adjusting the templates involves a lot more than |
| 15 |
just styling a ``login.html`` and a ``signup.html`` template. Therefore, a |
| 16 |
mechanism is included that allows you to adjust the look and feel of all |
| 17 |
templates by only overriding a few core templates. This approach allows you to |
| 18 |
achieve visual results fast, but is of course more limited compared to styling |
| 19 |
all templates yourself. |
| 20 |
|
| 21 |
|
| 22 |
Overriding the Built-In Templates |
| 23 |
--------------------------------- |
| 24 |
|
| 25 |
The ``allauth`` app includes all templates, and can be found in the |
| 26 |
`allauth/templates |
| 27 |
<https://codeberg.org/allauth/django-allauth/src/branch/main/allauth/templates>`__ |
| 28 |
directory. When ``allauth`` is part of your ``INSTALLED_APPS``, and |
| 29 |
``"APP_DIRS": True`` is configured, Django will be able to find its templates. |
| 30 |
As ``DIRS`` is searched before ``APP_DIRS``, overriding the templates involves |
| 31 |
adding an entry to ``DIRS`` that points to your a project specific template |
| 32 |
folder, as follows:: |
| 33 |
|
| 34 |
from pathlib import Path |
| 35 |
|
| 36 |
BASE_DIR = Path(__file__).resolve().parent.parent |
| 37 |
|
| 38 |
TEMPLATES = [ |
| 39 |
{ |
| 40 |
"BACKEND": "django.template.backends.django.DjangoTemplates", |
| 41 |
"DIRS": [ |
| 42 |
BASE_DIR / "templates" |
| 43 |
], |
| 44 |
"APP_DIRS": True, |
| 45 |
"OPTIONS": { |
| 46 |
"context_processors": [ |
| 47 |
"django.template.context_processors.debug", |
| 48 |
"django.template.context_processors.request", |
| 49 |
"django.contrib.auth.context_processors.auth", |
| 50 |
"django.contrib.messages.context_processors.messages", |
| 51 |
], |
| 52 |
}, |
| 53 |
}, |
| 54 |
] |
| 55 |
|
| 56 |
If you copy over all templates to your ``BASE_DIR / "templates"`` it should |
| 57 |
contain these entries (a.o.): |
| 58 |
|
| 59 |
- An ``account`` folder containing the templates from the ``allauth.account`` app. |
| 60 |
- A ``socialaccount`` folder containing the templates from the ``allauth.socialaccount`` app. |
| 61 |
- A ``mfa`` folder containing the templates from the ``allauth.mfa`` app. |
| 62 |
- An ``allauth`` folder containing the overall styling templates (see the next section). |
| 63 |
|
| 64 |
|
| 65 |
Styling the Existing Templates |
| 66 |
------------------------------ |
| 67 |
|
| 68 |
Instead of copying all templates, a mechanism is included that allows you to |
| 69 |
adjust the look and feel of all templates by only overriding a few core |
| 70 |
templates. This approach allows you to achieve visual results fast, but is of |
| 71 |
course more limited compared to styling all templates yourself. |
| 72 |
|
| 73 |
|
| 74 |
Layouts |
| 75 |
^^^^^^^ |
| 76 |
|
| 77 |
The existing templates use two base page layouts: |
| 78 |
|
| 79 |
- The entrance layout: These are all pages where the user is in the process of |
| 80 |
authenticating, such as the login and signup pages. |
| 81 |
|
| 82 |
- The account management layout: The pages where an authenticated user can |
| 83 |
manage the account, such as changing the email address or password. |
| 84 |
|
| 85 |
You can alter these layouts by providing these templates in your own project: |
| 86 |
|
| 87 |
========================================== =========== |
| 88 |
Template file Description |
| 89 |
========================================== =========== |
| 90 |
allauth/layouts/base.html The overall base template. |
| 91 |
allauth/layouts/entrance.html The entrance template, extending the base template. |
| 92 |
allauth/layouts/manage.html The account management template, extending the base template. |
| 93 |
========================================== =========== |
| 94 |
|
| 95 |
|
| 96 |
Elements |
| 97 |
^^^^^^^^ |
| 98 |
|
| 99 |
When rendering e.g. a Bootstrap button you would typically use:: |
| 100 |
|
| 101 |
<button class="btn btn-primary">Okay</button> |
| 102 |
|
| 103 |
Yet, when a different CSS framework is used other class names apply, and |
| 104 |
possibly even other markup. Therefore, the built-in templates do not include the |
| 105 |
above content directly. Instead of referring to tags such ``<button>``, ``<h1>`` |
| 106 |
or ``<form>`` directly, the templates render those elements using a special |
| 107 |
element `templatetag`:: |
| 108 |
|
| 109 |
{% load allauth %} |
| 110 |
{% element h1 tags="foo,bar" %}Welcome{% endelement %} |
| 111 |
|
| 112 |
Under the hood, this `templatetag` renders the ``allauth/elements/h1.html`` |
| 113 |
template, which out of the box contains this:: |
| 114 |
|
| 115 |
{% load allauth %}<h1>{% slot %}{% endslot %}</h1> |
| 116 |
|
| 117 |
If you want to change the styling of all headings across all pages, you can do |
| 118 |
so by overriding that ``allauth/elements/h1.html`` template, as follows:: |
| 119 |
|
| 120 |
{% load allauth %} |
| 121 |
<div class="myproject-h1 aa-{{ origin|slugify }}" |
| 122 |
style="font-size: {% if "foo" in attrs.tags %}3{% else %}5{% endif %}rem"> |
| 123 |
{% slot %}{% endslot %} |
| 124 |
</div> |
| 125 |
|
| 126 |
Of course, the above is a bit of a contrived example. In each of the element |
| 127 |
templates the ``{{ origin }}`` context variable is available, which is equal to |
| 128 |
the base template name where the element is used (e.g. ``account/login`` for |
| 129 |
elements used from within the ``account/login.html`` template). |
| 130 |
|
| 131 |
Slots may also be named. In that case, the ``element`` will be invoked like:: |
| 132 |
|
| 133 |
{% load allauth %} |
| 134 |
{% element form method="post" action=action_url %} |
| 135 |
{% slot body %} |
| 136 |
... |
| 137 |
{% endslot %} |
| 138 |
{% slot actions %} |
| 139 |
... |
| 140 |
{% endslot %} |
| 141 |
{% endelement %} |
| 142 |
|
| 143 |
When overriding an element with named slots, they may be injected in any order. |
| 144 |
For example, with ``allauth/elements/form.html``:: |
| 145 |
|
| 146 |
{% load allauth %} |
| 147 |
<form method="{{ attrs.method }}" action="{{ attrs.action }}"> |
| 148 |
{% slot body %} |
| 149 |
{% endslot %} |
| 150 |
<hr> |
| 151 |
{% slot actions %} |
| 152 |
{% endslot %} |
| 153 |
</form> |
| 154 |
|
| 155 |
The following elements are available -- override them as you see fit for your |
| 156 |
project: |
| 157 |
|
| 158 |
========================================== =========== |
| 159 |
Template file Description |
| 160 |
========================================== =========== |
| 161 |
allauth/elements/alert.html Display alert messages. |
| 162 |
allauth/elements/badge.html Badges for labeling purposes. |
| 163 |
allauth/elements/button.html A button (``<button>``). |
| 164 |
allauth/elements/button_group.html A group of related buttons. |
| 165 |
allauth/elements/details.html Disclosure (``<details>``). |
| 166 |
allauth/elements/field.html A single form field. |
| 167 |
allauth/elements/fields.html The form fields, uses ``{{form.as_p}}`` by default, hence, not rendering the ``field.html``. |
| 168 |
allauth/elements/form.html The ``<form>`` container tag. |
| 169 |
allauth/elements/h1.html Level 1 heading (``<h1>``). |
| 170 |
allauth/elements/h2.html Level 2 heading (``<h2>``). |
| 171 |
allauth/elements/hr.html Horizontal rule (``<hr>``). |
| 172 |
allauth/elements/img.html An image tag (``<img>``). |
| 173 |
allauth/elements/panel.html A panel (aka card), consisting of a title, body and actions. |
| 174 |
allauth/elements/p.html Paragraphs (``<p>``). |
| 175 |
allauth/elements/provider.html A link to a third-party provider. |
| 176 |
allauth/elements/provider_list.html The container element for the list of third-party providers. |
| 177 |
allauth/elements/table.html Table (``<table>``). |
| 178 |
allauth/elements/tbody.html Table body (``<tbody>``). |
| 179 |
allauth/elements/td.html Table data cell (``<td>``). |
| 180 |
allauth/elements/th.html Table header cell (``<th>``). |
| 181 |
allauth/elements/thead.html Table head (``<thead>``). |
| 182 |
allauth/elements/tr.html Table row (``<tr>``). |
| 183 |
========================================== =========== |
| 184 |
|
| 185 |
|
| 186 |
Example |
| 187 |
^^^^^^^ |
| 188 |
|
| 189 |
The source repository contains a Bootstrap styled example project, which |
| 190 |
provides a good example of how all of the above can be put together to provide |
| 191 |
styling without altering any of the content templates. Please take a look at |
| 192 |
the `templates of the example project |
| 193 |
<https://codeberg.org/allauth/django-allauth/src/branch/main/examples/regular-django/example/templates>`__. |
| 194 |
You can see those templates live in the `running demo project |
| 195 |
<https://django.demo.allauth.org>`__. |