User Registration in Django (the simplest way to register user)

Опубликовано: 16 Апрель 2023
на канале: Developer Bites
16
1

Django provides the python class UserCreationForm to register user. This class generates the required HTML and also gives the functionality for form validation. You can import the class and type the code like this:

django.contrib.auth.forms import UserCreationForm

def user_register(request):
"to provide user registration functionality"
if request.method=='POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect(reverse('success_register'))
else:
return render(request, "registration/user_register.html", {'form':form})
form = UserCreationForm()
return render(request, "registration/user_register.html", {'form':form})


After the user is successfully registered, a redirect can be made to a success page:

def success_register(request):
"to show after successful registration"
return render(request, "registration/success_register.html", {})

You need to create HTML files like user_register.html and success_register.html and saved the files in the folder templates/registration as shown in the video.

The above functionality will work correctly provided you are using the default authentication and not any custom authentication.

The tutorial uses python-3.8 and Django-4.1.4