django-simple-captcha不显示验证码字段

时间:2014-02-11 04:51:51

标签: django django-forms

您是否有提示为什么验证码不显示?

from captcha.fields import CaptchaField

class ResponseForm(models.ModelForm):

    captcha = CaptchaField() # added this according to documentation

    class Meta:
        model = Response
        fields = ['comments']

    def __init__(self, *args, **kwargs):
        [...]

    def save(self, commit=True):
        [...]

它在这里工作:

from captcha.fields import CaptchaField

class UserCreationForm(UserCreationFormBase):

    email = forms.EmailField(
        label='Email',
        error_messages={'invalid': 'Check email address!'})

    captcha = CaptchaField() # works fine here

    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2')

    def get_credentials(self):
        [...]

是因为功能def __init__和/或def save吗?

行为是没有显示验证码字段,并且表单在点击提交时不执行任何操作。如果代码中没有captcha,它就可以正常工作。

更新

没有错误消息。似乎页面再次加载。

控制台输出:POST /umfragen/testumfrage-2/ HTTP/1.1 200 2661

完整def __init__

        # expects a survey object passed in initially
        survey = kwargs.pop('survey')
        self.survey = survey
        super(ResponseForm, self).__init__(*args, **kwargs)
        self.uuid = random_uuid = uuid.uuid4().hex

        # adds a field for each survey question,
        # corresponding to the question type
        data = kwargs.get('data')
        for q in survey.questions():
            if q.question_type == Question.TEXT:
                self.fields['question_%d' % q.pk] = forms.CharField(
                    label=q.question_text,
                    widget=forms.Textarea)
            elif q.question_type == Question.RADIO:
                # calls function get_choices from models.py 
                question_choices = q.get_choices()
                self.fields['question_%d' % q.pk] = forms.ChoiceField(
                    label=q.question_text,
                    widget=forms.RadioSelect,
                    choices=question_choices)
            elif q.question_type == Question.SELECT:
                question_choices = q.get_choices()
                question_choices = tuple([('', '- - - - -')]) + question_choices
                self.fields['question_%d' % q.pk] = forms.ChoiceField(
                    label=q.question_text,
                    widget=forms.Select,
                    choices=question_choices)
            elif q.question_type == Question.SELECT_MULTIPLE:
                question_choices = q.get_choices()
                self.fields['question_%d' % q.pk] = forms.MultipleChoiceField(
                    label=q.question_text,
                    widget=forms.CheckboxSelectMultiple,
                    choices=question_choices)

            # adds category as a data attribute and allows sorting the questions by category
            if q.category:
                classes = self.fields['question_%d' % q.pk].widget.attrs.get('class')
                if classes:
                    self.fields['question_%d' % q.pk].widget.attrs['class'] = classes + ('cat_%s' %  q.category.name)
                else:
                    self.fields['question_%d' % q.pk].widget.attrs['class'] = ('cat_%s' %  q.category.name)
                self.fields['question_%d' % q.pk].widget.attrs['category'] = q.category.name

            # initializes the form field with values from a POST request    
            if data:
                self.fields['question_%d' % q.pk].initial = data.get('question_%d' % q.pk)

0 个答案:

没有答案