Mit Django generische CreateView kann ich ein neues Benutzerkonto erstellen, aber wie kann ich diesen Benutzer automatisch nach der Registrierung mit dieser Technik anmelden?Django: Wie Benutzer direkt nach der Registrierung mit generischen CreateView anmelden
urls.py ...
url (r '^ Anmeldungen/$', 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
Ja, das ist genau die gleiche Lösung, die ich gefunden habe. Ich habe auch etwas Zeit verloren, um Hash-Pass zur Authentifizierung zu verwenden)) –