1def signup(request):
2 if request.method == 'POST':
3 form = UserCreationForm(request.POST)
4 if form.is_valid():
5 form.save()
6 username = form.cleaned_data.get('username')
7 password = form.cleaned_data.get('password1')
8 user = authenticate(username=username, password=password)
9 login(request, user)
10 return redirect('home')
1from django.contrib.auth import authenticate
2
3class SignUpView(CreateView):
4 form_class = UserRegistrationForm
5 success_url = reverse_lazy('home')
6 template_name = 'registration/signup.html'
7
8 def form_valid(self, form):
9 view = super(SignUpView, self).form_valid(form)
10 username = form.cleaned_data.get('username')
11 password = form.cleaned_data.get('password1')
12 user = authenticate(username=username, password=password)
13 login(self.request, user)
14 return view