(Picture: https://www.pexels.com/photo/white-samsung-laptop-computer-showing-smartphones-163141/)
In this tutorial I will cover a few strategies to create Django user sign up/registration. Usually I implement it from scratch. You will see it’s very straightforward.
For the examples I will use an empty Django project named mysite. Inside the mysite folder I created an app named core. So every time you see mysite and/or core, change to the suitable project name and app name.
A brief summary of what you are going to find here:
The most simple way to implement a user sign up is by using the UserCreationForm as it is. This strategy is suitable in case you are using the default Django user, using username to authenticate and is interested only in setting the username and password upon sign up.
urls.py
Now in the view, just change the form class to use our new SignUpForm.
views.py
In this particular case, the profile is created using a Signal. It’s not mandatory, but usually it is a good way to implement it. You can learn more about extending the user model in this post.
Let’s say we want to also get the birth_date of the user upon sign up. First, let’s update the model form:
forms.py
Here we do all the magic, checking if the user exists, if the token is valid. If everything checks, we switch the flags is_active and email_confirmed to True and log the user in.
By changing the value of the email_confirmed field, it will cause the link to be invalidated.
That’s it! I hope you enjoyed this post. In a future post I can explore the third party libraries that help the registration process. But usually I prefer to implement it by myself, and avoid an extra dependency.
All the four examples are available on GitHub in the same repository: