Welcome back to another Django series where we learn how to create a registration portal in a few lines of code. Django has built-in authentication APIs with a form that accelerates the rate of development. While dealing with a large project, create a new app to deal with registration and login, to avoid messing up.
Prefer to call the app a name related to what its function is doing. As for the portal, you might call it customers, users, or any related names. Then create a form.py where you will define the fields required. The Django authentication form comes with several fields ie username, email, password1, password2. Visit my recent blog where I showed how to use forms. (URLS)
from django.contrib.auth.forms import UserCreationForm
from Django.contrib.auth.models import User
from django import forms
class CreateUserForm(UserCreationForm):
first_name = forms.CharField(max_length=100)
last_name = forms.CharField(max_length=100)
class Meta:
model = User
fields = [‘first_name’, ‘last_name’, ‘username’,
’email’, ‘password1’, ‘password2’]
In my case, I want to include firstname and lastname, which are the missing fields in django authentication form. Therefore, you must create the fields separately before including them in the fields. Otherwise, you will receive an error. The user is an already built API, it comes with the package when we install django. You are not limited to this, but you can create your own in the model.py, let’s say customers, managers, and supervisors.
At views.py, we write a function with our name and registration. The function should validate the entered data before saving it to the database.
from django.shortcuts import render, redirect
from .forms import CreateUserForm, LoginForm
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import Group
@unauthenticated_user
def registeredPage(request):
form = CreateUserForm()
if request.method == “POST”:
form = CreateUserForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get(‘username’)
messages.success(request, “Account created for”+username)
return redirect(‘login’)
context = {‘form’: form}
return render(request, “hello/register.html”, context)
Line1: Create a function called registeredPage with a requiest as an argument.
Line2: The variable form takes CReateUserForm which was imported from form.py. It will create a dictionary with all fields.
Line3: Use of if to declare the method you want to use. In this case, we use POST, so that it can post data to our database.
Line4: Here we again update the form with data from the user. argument request.POST takes the data from the templates fields.
Line5: This if statement is used to validate the data entered. Django will do that automatically, eg To make sure password1=pasword2.
Line6: It saves your data in the database after validation.
Line7; After the data has been saved, we can access all the fields using their respective names.
Line8; Make sure you have imported the message package from Django. auth. This will trigger a successful text on the next page where it will land.
Line9; This line will direct the user to the next page if all the entered details are correct.
Line10: This is always among the first lines that you should write. It’s a python dictionary with form: form, which will carry all the fields to the templates.
Line11; The last line is where you declare your registration template. Make sure to include the context dictionary.
We are done registering the user. Let’s look at the template for registration.
Use the cover image for template.
Make sure your content goes inside a form with method=POST and action=”. At the bottom, you might observe the form. errors. Just include, it’s important because it will tell you the field that is having a mistake. Password1 stands for your chosen password, Password 2 represents the RE-type or confirm password.
Top comments (0)