django sign up 1

django sign up

def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect('home')

Here is what the above code is Doing:
1. We import the UserCreationForm class from django.contrib.auth.forms.
2. We create a view called signup that takes a request as a parameter.
3. We check if the request method is POST.
4. If it is, we create an instance of UserCreationForm and pass it the POST data.
5. We check if the form is valid.
6. If it is, we save the form and create a new user.
7. We then log the user in and redirect them to the home page.

Similar Posts