Tutorial Wagtail Version: 2.x
Learn how to add user authentication and give your users the ability to signup, login, reset their password and confirm their email addresses.
In the land of Python, Django and Wagtail there are tonnes of packages we can use. Lots of great developers open source their code and let other people contribute to the original code base and make a package truly amazing.
One of these packages is Django Allauth — the package we'll be using today.
We need to install the package first. Make sure you enter your virtual environment and install django-allauth.
$ pip install django-allauth
Installing django-allauth requires a few steps, but it's mostly copy and paste. Open your base.py
settings file and add the following:
Also in your base.py
file you'll want to add the allauth authentication backend. Add the following to your main settings file.
Next we'll need to make sure we have 6 apps enabled. Three are from Django itself, and three are from Allauth. In your base.py
settings file, make sure you have each of these apps installed.
We also need to tell Allauth which site to enable authentication on. If you're not using Django or Wagtail multisite, then the default SITE_ID will always be 1. Make sure this is also in your base.py file.
Almost there! We're done with the base.py file for a moment. Now you'll need to add the Allauth URLs to your application. If you don't do this, then Allauth will be installed but it won't be accessible from the web (making all of our previous work useless).
Open urls.py
and add url(r'', include('allauth.urls')),
to your urlpatterns
.
Last step before we can configure our websites' new authentication system. If you were to run python manage.py runserver 0:8000
you should be shown a bunch of red text saying "You have 7 unapplied migrations" (the actual number may vary depending on the version of django-allauth). Django Allauth lets us create email-based registration and also social-based authentication. Internally, it has it's own way of dealing with user accounts and storing user data. So we need to run migrations now.
$ python manage.py migrate
And just like that, we're up and running! To test this go to http://localhost:8000/login/ (or http://localhost:8000/accounts/login/ if you used the default Allauth URL pattern).
You should see a login form.
If the page loads, but there's nothing being displayed, you'll need to modify the allauth templates. We'll tackle this at the end of the tutorial.
In the video I configured our authentication package using common configuration settings. I didn't use them all and there are more at https://docs.allauth.org/en/latest/account/configuration.html — make sure you read through all the settings and change the settings to fit your websites needs.
Let's re-open our base.py
settings and add some generic settings.
I won't go over what each of these settings means because it's all nicely laid out in the django-allauth documentation. Here's the link again in case you feel like reading it right now.
When you want to make changes to the Django Allauth templates, like the default (and ugly) login page or the sign up page, you'll want to copy the package templates into your own template files. Here's a list of all the regular template files you can use: https://github.com/pennersr/django-allauth/tree/master/allauth/templates/account
Basically, you can copy the contents from /allauth/templates/account/login.html into your main templates folder, just make sure the folder structure matches django-allauth's directory structure.
For example, if you wanted to change the login.html template:
So if you copy the original login.html file from django-allauth into your project, you can now modify it as much as you need.
In your templates you can now detect if a user is logged in or if the user is a guest. It's as easy as an if/else statement.
Additionally, you can add direct URLs in your template the any Allauth view. If you view this link, you can see all the available URL names. This is where I got {% url 'account_login' %}
and {% url 'account_logout' %}
from. There are many others you'll want to implement as well, such as the sign up url.
Whenever you add a new package to your project, make sure you update the appropriate requirements file. In my case, I have a requirements.txt file so I've added django-allauth to it, and pinned it to the most recent version.
Adding Global Site Settings to Your Wagtail Website
Posted on
Most websites have social media icons in their footer and often several other links and icons that are global across the entire website. In this video we explore how to enable that with Wagtails settings context processor.
How to add localization to your website
Posted on
Learn how to make your Wagtail website multilingual with different translations
Adding a Bootstrap 4 Theme to Our Wagtail Website
Posted on
Learn how to quickly add a free Bootstrap 4 theme to your Wagtail CMS website. With just one line of code we can import and entire theme. We'll also add styling, a navbar, and a background image.
Adding Recaptcha to Your Contact Forms
Posted on
Contact forms are notorious for collecting spam from bots. In this tutorial we'll install a package to automatically add Google Recaptcha to our contact forms. We'll explore how to get the API keys, as well.
This course covers everything from basic installation to advanced features like custom blocks and API integration, it's perfect for developers looking to enhance their skills with this powerful CMS.