You can register or create new users in django for the User Model
as follows.
First, you need to create the User Model
. This can be done by running the python manage.py migrate
command. All the default django models would be created when you run this command.
python manage.py migrate
Next open the authentication/views.py
file and add the code to create a new User. You can use the UserCreationForm
that django forms offers in order to create a new user. That form would be passed along the template
when a GET
request is called to the url. On a POST
request, the form data would be saved and the user would be logged by using the login()
function from django auth
module, and then the user would be redirected to the home
page.
from django.shortcuts import render, redirect
from django.contrib.auth import login
from django.contrib.auth.forms import UserCreationForm
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect('home')
else:
form = UserCreationForm()
return render(request, 'register.html', {'form': form})
Also, lets create a route for the register
function, edit the authentication/urls.py
as follows :
path("register", views.register,name='register'),
Along with that, you should create a register.html
template in order to handle the form. You can display the UserCreationForm
as a paragraph
in the html template as follows. The form method is set to POST
, so that it sends a POST
request when the form is submitted. The {% csrf_token %}
should be present to ensure that the form is submitted through our website and not from any other source.
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Register</title>
</head>
<body>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Register</button>
</form>
</body>
</html>
Now when you visit http://127.0.0.1:8000/register, you will see a web page as follows.
By entering the details, it will allow you to create a new user.
Click the
register
button, in order to create a new user. After the register
button is clicked it will login
the user and redirect to the home
page.
In order to verify which user is logged in, you can display the user in the
home.html
page as follows :
<h1>Home Page</h1>
<h2>Welcome {{request.user}}</h2>
<h2>Restricted content to be accessed by only authenticated users</h2>
Now when you open the website, you can see the username
along with it.
Also, if you try to create a new user with the same
username
, it will show an error.
These validations are handled by django internally along with the password validation. The password validation is based on the
AUTH_PASSWORD_VALIDATORS
field in the settings.py
file. The default value of the field would be like this :
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
You can remove or add some validations by editing the AUTH_PASSWORD_VALIDATORS
in settings.py
file.
For example, if you want to allow that a password can be entirely numeric, then you can remove the NumericPasswordValidator
from the list.
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
]
Now the website would be as follows :
As you can see the text that
Your password can't be entirely numeric
is now not present. Now you can create a new user whose password can even be entirely numeric.