每 3 POST 个请求向 Django Forms 添加字段
Add field to Django Forms each 3 POST request
我正在尝试建立一个网站,该网站的表单很小,只有一个可见字段:EmailField。我正在使用 a python package 来使用 Google No CAPTCHA ReCATPCHA,我希望验证码 (NoReCaptchaField) 仅在用户正确提交表单 3 次后出现。
在我的 views.py 中,我将创建一个传递布尔值 needs_captcha 的表单,每 3 个成功的表单 POST 请求(这是正确的)。
form = JoinForm(needs_captcha)
现在在我的 forms.py 中有以下代码:
class JoinForm(forms.ModelForm):
def __init__(self, needs_captcha, *args, **kwargs):
super(JoinForm, self).__init__(*args, **kwargs)
if needs_captcha:
self.captcha = NoReCaptchaField(gtag_attrs={'data-theme': 'light'})
print("captcha will be included")
else:
print("captcha won't be included")
# this is the only field that is actually filled by the user
email = forms.EmailField(max_length=128, help_text="Introduce la cuenta de gmail que tienes asociada a tu"
" dispositivo Android",
widget=forms.EmailInput(attrs={'class': "w-input email_input",
'placeholder': 'Tu cuenta de Google'}),
required=True)
.....
def clean(self):
.....
def clean_email(self):
.....
return email
class Meta:
model = InterestedUser
fields = ('email', 'name', 'subject', 'via', 'content',)
因为不是所有的 JoinForm 对象都应该有字段我试图在 init 中添加字段只是为了我为此特定请求创建的对象但它只是没有不行,验证码不会出现。这种做法正确吗?
根据我的经验,你不能在 super().init() 之后添加新字段。并且字段在 _init 之前不存在。因此,解决方法是始终包含可选字段,然后在不需要时在初始化后将其删除。
class JoinForm(forms.ModelForm):
captcha = NoReCaptchaField(gtag_attrs={'data-theme': 'light'})
class Meta:
model = InterestedUser
fields = ('email', 'name', 'subject', 'via', 'content',)
def __init__(self, needs_captcha, *args, **kwargs):
super().__init__(*args, **kwargs)
if not needs_captcha:
del self.fields['captcha']
我正在尝试建立一个网站,该网站的表单很小,只有一个可见字段:EmailField。我正在使用 a python package 来使用 Google No CAPTCHA ReCATPCHA,我希望验证码 (NoReCaptchaField) 仅在用户正确提交表单 3 次后出现。
在我的 views.py 中,我将创建一个传递布尔值 needs_captcha 的表单,每 3 个成功的表单 POST 请求(这是正确的)。
form = JoinForm(needs_captcha)
现在在我的 forms.py 中有以下代码:
class JoinForm(forms.ModelForm):
def __init__(self, needs_captcha, *args, **kwargs):
super(JoinForm, self).__init__(*args, **kwargs)
if needs_captcha:
self.captcha = NoReCaptchaField(gtag_attrs={'data-theme': 'light'})
print("captcha will be included")
else:
print("captcha won't be included")
# this is the only field that is actually filled by the user
email = forms.EmailField(max_length=128, help_text="Introduce la cuenta de gmail que tienes asociada a tu"
" dispositivo Android",
widget=forms.EmailInput(attrs={'class': "w-input email_input",
'placeholder': 'Tu cuenta de Google'}),
required=True)
.....
def clean(self):
.....
def clean_email(self):
.....
return email
class Meta:
model = InterestedUser
fields = ('email', 'name', 'subject', 'via', 'content',)
因为不是所有的 JoinForm 对象都应该有字段我试图在 init 中添加字段只是为了我为此特定请求创建的对象但它只是没有不行,验证码不会出现。这种做法正确吗?
根据我的经验,你不能在 super().init() 之后添加新字段。并且字段在 _init 之前不存在。因此,解决方法是始终包含可选字段,然后在不需要时在初始化后将其删除。
class JoinForm(forms.ModelForm):
captcha = NoReCaptchaField(gtag_attrs={'data-theme': 'light'})
class Meta:
model = InterestedUser
fields = ('email', 'name', 'subject', 'via', 'content',)
def __init__(self, needs_captcha, *args, **kwargs):
super().__init__(*args, **kwargs)
if not needs_captcha:
del self.fields['captcha']