Sunday, June 26, 2011

a problem about the custom login form with a verify code filed.

I want to add a verify code field in the login form.
So, I write a authentication_form which added a charField to generate a verify code and the verify code saved to the request.session.
But When I send login form request,I find in my authentication_form, I can't campare the session's verify code and the clean_data["verifycode"],
Because the request in Null when invoke my authentication_form.

code below and the question in line highlighted like ###HERE
  class MyAuthenticationForm(AuthenticationForm):      verifyimg=forms.CharField(label="verifycode",widget=vericonf.CaptchaWidget)                  def __init__(self, request=None,*args, **kwargs):         self.request=request         kwargs_new = {'error_class': DivErrorList}         kwargs.update(kwargs_new)         #elf.error_class = DivErrorList         super(MyAuthenticationForm,self).__init__(*args, **kwargs)                def as_div(self):         "Returns this form rendered as HTML div."         return self._html_output(             normal_row = u'<div%(html_class_attr)s style="float:left;"><div class="formlabel">%(label)s</div><div class="formfield">%(field)s%(help_text)s</div><div class="formerror">%(errors)s</div></div>',             error_row = u'<div>%s</div>',             row_ender = u'</div></div>',             help_text_html = u'<div style="float:left;"><span class="helptext">%s</span></div>',             errors_on_separate_row = False)          def clean_verifyimg(self):         pass              def clean(self):         vericode=""         username = self.cleaned_data.get('username')         password = self.cleaned_data.get('password')                  if self.request and self.request.session and self.request.session.get("verifyimg"):             vericode = self.request.session.get("verifyimg")                  print vericode  ###HERE request.session is Null then I can't compare vericode and verify now.How to do it?                      verify=self.cleaned_data.get('verifyimg')         print verify          if username and password:             self.user_cache = authenticate(username=username, password=password)             if self.user_cache is None:                 raise forms.ValidationError(_("Please enter a correct username and password. Note that both fields are case-sensitive."))             elif not self.user_cache.is_active:                 raise forms.ValidationError(_("This account is inactive."))         if verify and vericode:             if verify != vericode:                 raise forms.ValidationError("verify code is wrong.pls Try again!")                  self.check_for_test_cookie()                  return self.cleaned_data


--
"OpenBookProject"-开放图书计划邮件列表
详情: http://groups.google.com/group/OpenBookProject
维基: http://wiki.woodpecker.org.cn/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment