With django generic CreateView I can create a new user account, but how can I login this user automatically after registration 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
Run the command py manage.py createsuperuser in Windows Command Prompt and python3 manage.py createsuperuser in Mac Terminal. Then fill out the username and password. Go to the http://127.0.0.1:8000/admin/ URL where you will see a Django administration login.
from django. contrib. auth import authenticate user = authenticate(username='john', password='secret') if user is not None: if user. is_active: print "You provided a correct username and password!" else: print "Your account has been disabled!" else: print "Your username and password were incorrect."
The Django authentication system handles both authentication and authorization. Briefly, authentication verifies a user is who they claim to be, and authorization determines what an authenticated user is allowed to do. Here the term authentication is used to refer to both tasks.
You can't manually check the password. Because when you are creating a user, django is storing the user's password as a hash value in the database. Now if you are storing the raw password in your custom table which is myuser , it's not a good practice.
it's maybe late but that was exactly my question, and after some hours of struggling finally find out.
Maybe you found but if other people are looking for a solution, here is mine.
You just have to override form_valid()
in your class Inheriting CreateView. Here is the 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 first catch the value of my parent class method form_valid()
in valid
, because when you call it, it calls the form.save(), which register your user in database and populate your self.object
with the user created.
After that I had a long problem with my authenticate, returning None
. It's because I was calling authenticate()
with the django hashed password, and authenticate hash it again.
Explaining this for you to understand why I use form.cleaned_data.get('username')
and not self.object.username
.
I hope it helps you or other, since I didn't find a clear answer on the net.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With