signup code in django code example

Example 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')

Example 2: signup view

from django.contrib.auth import authenticate

class SignUpView(CreateView):
    form_class 		= UserRegistrationForm
    success_url 	= reverse_lazy('home')
    template_name	= 'registration/signup.html'
    
    def form_valid(self, form):
        view        = super(SignUpView, self).form_valid(form)
        username    = form.cleaned_data.get('username')
        password    = form.cleaned_data.get('password1')
        user        = authenticate(username=username, password=password)       
        login(self.request, user)
        return view