0

我想在创建用户后登录。但是我正在使用表单向导,当我尝试时出现错误“未定义全局名称'请求'”。

这是我的观点.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

那么创建用户后我应该怎么做才能登录呢?

编辑:在进行 user777466 建议的编辑后,我的 views.py 现在是:

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)

表格期间和之后的输出是:

[2013 年 11 月 13 日 08:39:12]“GET /gestionProfil/createNew/HTTP/1.1”200 4639

[2013 年 11 月 13 日 08:39:22]“POST /gestionProfil/createNew/HTTP/1.1”200 5085

[2013 年 11 月 13 日 08:39:40]“POST /gestionProfil/createNew/HTTP/1.1”200 4601

[2013 年 11 月 13 日 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收到一个天真的日期时间(2013-11-13 08:39:45.847325)而时间区域支持处于活动状态。运行时警告)

/usr/local/lib/python2.7/dist-packages/django/db/models/fields/init .py:827: RuntimeWarning:DateTimeField收到一个天真的日期时间(2013-11-13 08:39:45.864404)而时间区域支持处于活动状态。运行时警告)

[2013 年 11 月 13 日 08:39:47]“POST /gestionProfil/createNew/HTTP/1.1”200 3704

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

4

1 回答 1

0

您的请求未通过 process_form_data 方法传递。

解决方案 1 - 在 process_form_data 方法中记录用户:

form_data = process_form_data(self.request, form_list)
def process_form_data(request, form_list)
  ...

解决方案 2 - 在 done 方法中登录用户:

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() 无法处理项目。在这种情况下,做一个简单的:

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

所以你应该有类似的东西:

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
于 2013-11-11T23:47:54.960 回答