I would like to login after the creation of users. But i'm using formwizard and I've got an error "global name 'request' is not defined" when i try.
Here is my view.py
class ProfilWizard(SessionWizardView):
template_name = "gestionProfil/profil-step1.html"
def done(self, form_list, **kwargs):
form_data = process_form_data(form_list)
return render_to_response('gestionProfil/profil.html', {'form_data': form_data})
def process_form_data(form_list):
form_data = [form.cleaned_data for form in form_list]
username = form_data[0]['username']
password = form_data[0]['password']
email = form_data[0]['email']
user = User.objects.create_user(username, email, password)
user.first_name = form_data[1]['firstName']
user.last_name = form_data[1]['lastName']
user.save()
user = authenticate(username = username, password = password)
if user:
login(request, user)
return form_data
So how should i do to login after the creation of users?
Edit : After making the edit suggested by user777466 my views.py is now:
class ProfilWizard(SessionWizardView):
template_name = "gestionProfil/profil-step1.html"
def done(self, form_list, **kwargs):
(form_data,user) = process_form_data(form_list)
user.backend = 'django.contrib.auth.backends.ModelBackend'
if user:
login(self.request, user)
#return HttpResponse(form_data[0]['password'])
return render_to_response('gestionProfil/profil.html', {'form_data': form_data})
def process_form_data(form_list):
form_data = [form.cleaned_data for form in form_list]
username = form_data[0]['username']
password = form_data[0]['password']
email = form_data[0]['email']
user = User.objects.create_user(username, email, password)
user.first_name = form_data[1]['firstName']
user.last_name = form_data[1]['lastName']
user.save()
return (form_data,user)
The output during and after the form are :
[13/Nov/2013 08:39:12] "GET /gestionProfil/createNew/ HTTP/1.1" 200 4639
[13/Nov/2013 08:39:22] "POST /gestionProfil/createNew/ HTTP/1.1" 200 5085
[13/Nov/2013 08:39:40] "POST /gestionProfil/createNew/ HTTP/1.1" 200 4601
[13/Nov/2013 08:39:40] "GET /captcha/image/c9adfbd5b6984ed094fdc7c4607acfb4d915d037/ HTTP/1.1" 200 4611
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/init.py:827: RuntimeWarning: DateTimeField received a naive datetime (2013-11-13 08:39:45.847325) while time zone support is active. RuntimeWarning)
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/init.py:827: RuntimeWarning: DateTimeField received a naive datetime (2013-11-13 08:39:45.864404) while time zone support is active. RuntimeWarning)
[13/Nov/2013 08:39:47] "POST /gestionProfil/createNew/ HTTP/1.1" 200 3704
Edit 2: I've remove my database and made a new syncdb, now the authentication is working.
Your request is not passed through the process_form_data method.
Solution 1 - log the user in the process_form_data method:
form_data = process_form_data(self.request, form_list)
def process_form_data(request, form_list)
...
Solution 2 - log the user in the done method:
class ProfilWizard(SessionWizardView):
template_name = "gestionProfil/profil-step1.html"
def done(self, form_list, **kwargs):
form_data = process_form_data(form_list)
user = authenticate(username = form_data[0]['username'], password = form_data[0]['password'])
if user:
login(self.request, user)
return render_to_response('gestionProfil/profil.html', {'form_data': form_data})
def process_form_data(form_list):
form_data = [form.cleaned_data for form in form_list]
username = form_data[0]['username']
password = form_data[0]['password']
email = form_data[0]['email']
user = User.objects.create_user(username, email, password)
user.first_name = form_data[1]['firstName']
user.last_name = form_data[1]['lastName']
user.save()
return form_data
Recently, I had the authenticate() not working on a project because of the password field. In that case, make a simple :
user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, user)
So you should have something like:
class ProfilWizard(SessionWizardView):
template_name = "gestionProfil/profil-step1.html"
def done(self, form_list, **kwargs):
form_data = process_form_data(self.request, form_list)
return render_to_response('gestionProfil/profil.html', {'form_data': form_data})
def process_form_data(self, request, form_list):
form_data = [form.cleaned_data for form in form_list]
username = form_data[0]['username']
password = form_data[0]['password']
email = form_data[0]['email']
user = User.objects.create_user(username, email, password)
user.first_name = form_data[1]['firstName']
user.last_name = form_data[1]['lastName']
user.save()
user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, user)
return form_data