2015-04-17 28 views
2

我在我的Django應用程序中使用包django-simple-captcha如何在Django中以脆弱的形式添加CaptchaField()?

documentation指出,添加在表單驗證碼現場,所有你需要做的是

from django import forms 
from captcha.fields import CaptchaField 

class CaptchaTestForm(forms.Form): 
    myfield = AnyOtherField() 
    captcha = CaptchaField() 

但是,我怎麼使用這樣的字段,如果我的形式是香脆的形式,這是如下 -

class SignupForm(authtoolsforms.UserCreationForm): 

    def __init__(self, *args, **kwargs): 
     super(SignupForm, self).__init__(*args, **kwargs) 
     self.helper = FormHelper() 
     self.fields["email"].widget.input_type = "email" 

     self.helper.layout = Layout(
      Field('email', placeholder="Enter Email", autofocus=""), 
      Field('name', placeholder="Enter Full Name"), 
      Field('password1', placeholder="Enter Password"), 
      Field('password2', placeholder="Re-enter Password"), 
      Field('gender', placeholder="Gender"), 
      Submit('sign_up', 'Sign up', css_class="btn-warning"), 
      ) 

回答

1
class SignupForm(authtoolsforms.UserCreationForm): 
    captcha = CaptchaField() 

    def __init__(self, *args, **kwargs): 
     super(SignupForm, self).__init__(*args, **kwargs) 
     self.helper = FormHelper() 
     self.fields["email"].widget.input_type = "email" 

     self.helper.layout = Layout(
      Field('email', placeholder="Enter Email", autofocus=""), 
      Field('name', placeholder="Enter Full Name"), 
      Field('password1', placeholder="Enter Password"), 
      Field('password2', placeholder="Re-enter Password"), 
      Field('gender', placeholder="Gender"), 
      Field('captcha ', placeholder="Enter captcha"), 
      Submit('sign_up', 'Sign up', css_class="btn-warning"), 
      )