关于python:使用formwizard-django创建用户后登录

Login after creating a user with formwizard-django

我想在创建用户后登录。但是我使用的是formwizard,我在尝试时遇到一个错误"global name'request'is not defined"。

这是我的视图.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

那么,在创建用户之后应该如何登录呢?

编辑:在进行用户777466建议的编辑之后,my views.py现在是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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)

表单期间和之后的输出为:

[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

编辑2:我已经删除了我的数据库并创建了一个新的同步数据库,现在身份验证正在工作。


您的请求没有通过进程表单数据方法传递。

解决方案1-将用户记录到进程表单数据方法:

1
2
3
form_data = process_form_data(self.request, form_list)
def process_form_data(request, form_list)
  ...

解决方案2-以"完成"方法记录用户:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

最近,由于密码字段的原因,我让authenticate()无法处理项目。在这种情况下,简单地说:

1
2
user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, user)

所以你应该有这样的东西:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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