Django: how to log in to a user immediately after registering using the shared CreateView

Using django generic CreateView, I can create a new user account, but how can I log in to this user automatically after registering using this technique?

urls.py

... url( r'^signup/$', SignUpView.as_view(), name = 'user_signup' ), ... 

views.py

 class SignUpView ( CreateView ) : form_class = AccountCreationForm template_name = 'accounts/signup.html' success_url = reverse_lazy( 'home' ) 

forms.py

 class AccountCreationForm ( forms.ModelForm ) : def __init__( self, *args, **kwargs ) : super( AccountCreationForm, self ).__init__( *args, **kwargs ) for field in self.fields : self.fields[field].widget.attrs['class'] = 'form-control' password1 = forms.CharField( label = 'Password', widget = forms.PasswordInput ) password2 = forms.CharField( label = 'Password confirmation', widget = forms.PasswordInput ) class Meta : model = User fields = ( 'email', 'first_name', ) def clean_password2 ( self ) : # Check that the two password entries match password1 = self.cleaned_data.get( "password1" ) password2 = self.cleaned_data.get( "password2" ) if password1 and password2 and password1 != password2: raise forms.ValidationError( "Passwords don't match" ) return password def save( self, commit = True ) : # Save the provided password in hashed format user = super( AccountCreationForm, self ).save( commit = False ) user.set_password( self.cleaned_data[ "password1" ] ) if commit: user.save() return user 
+9
source share
2 answers

maybe late, but that was exactly my question, and after several hours of struggle, they finally found out.

You may have found, but if other people are looking for a solution, here's mine.

You just need to override form_valid() in your class CreateView Inheritance. Here is an example with my own class:

 class CreateArtistView(CreateView): template_name = 'register.html' form_class = CreateArtistForm success_url = '/' def form_valid(self, form): valid = super(CreateArtistView, self).form_valid(form) username, password = form.cleaned_data.get('username'), form.cleaned_data.get('password1') new_user = authenticate(username=username, password=password) login(self.request, new_user) return valid 

I will first catch the value of the method of my parent class form_valid() in valid , because when you call it, it calls form.save (), which registers your user in the database and populates your user self.object .

After that, I had a big problem with my authentication, returning None . This is because I called authenticate() with the django hashed password and authenticated the hash again.

Explaining this, you understand why I use form.cleaned_data.get('username') , and not self.object.username .

I hope this helps you or another, as I have not found a clear answer on the net.

+16
source

In Django 2.2, I was not able to get it to work as published by Bestasttung . But I dealt with a small change to the form_valid method.

 class CreateAccountView(CreateView): template_name = 'auth/create_account.html' form_class = SignInForm success_url = '/' def form_valid(self, form): valid = super().form_valid(form) # Login the user login(self.request, self.object) return valid 
0
source

Source: https://habr.com/ru/post/1205281/


All Articles