Django-recaptcha 总是显示 "This field is required." 即使它在那里
Django-recaptcha always showing "This field is required." even-tough it is there
我正在尝试在我的一个项目中使用 Recaptcha v3。不幸的是,form.valid_date() 方法总是失败并显示“此字段为必填项”。
运行 django-recaptcha.
forms.py 看起来像这样:
class AccountForm(forms.ModelForm):
# adding some default validators
phone = forms.CharField(validators=[MinLengthValidator(10)], error_messages={'invalid':_("Please add a valid phone number.")})
terms = forms.BooleanField()
captcha = ReCaptchaField(
public_key=settings.RECAPTCHA_PUBLIC_KEY,
private_key=settings.RECAPTCHA_PRIVATE_KEY,
widget=ReCaptchaV3(
attrs={
'required_score': 0.85,
}
)
)
class Meta:
model = Account
fields = ['phone', 'terms']
def clean_phone(self):
data = self.cleaned_data['phone']
if not validate_phone(data):
raise forms.ValidationError(_("Please add a valid phone number. "))
return data
def clean_terms(self):
data = self.cleaned_data['terms']
if not True:
raise forms.ValidationError(_("Please accept the T&Cs"))
return data
views.py 看起来像这样:
def register_web(request: object) -> object:
key = request.GET.get('key', '')
merchant = request.GET.get('merchant', '')
if request.method == 'POST':
print(form.is_valid())
print(form.errors)
if form.is_valid():
phone = phone_filter(str(request.POST.get('phone')).replace(' ', ''))
name = request.POST.get('name', '')
confirmationForm = ConfirmationForm()
return render(request, 'registerWebConfirmation.html', {'context':context })
else:
return render(request, 'registerWeb.html', {'form': form})
else:
return render(request, 'registerWeb.html', {'form': form})
views.py 中的打印状态:
False
<ul class="errorlist"><li>captcha<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
模板如下所示:
<form method="POST" class="post-form">
{% csrf_token %}
{{ form.phone.errors }}
{% render_field form.phone placeholder=form.phone.label class+="form-control" %}
{% if form.name %}
<br /><br />{% render_field form.name placeholder=form.name.label class+="form-control" %}
<input type="hidden" name="advanced" value="1" />
{% endif %}
<br /><br />
{{ form.captcha }}
<input type="hidden" name="merchant" value="{{ merchant }}" />
<input type="hidden" name="key" value="{{ key }}"/>
<div class="d-flex cb-container">
{{ form.terms.errors }}
{% render_field form.terms placeholder=form.terms.label %}
<label for="id_terms">Accept <a href="/dataprivacy">T&Cs</a></label>
</div>
<input class="btnSubmit" type="submit" value="{% trans "Anmelden" %}">
</form>
欢迎提出任何想法。
我试图重现你的问题,正如你所说,表单的 is_valid()
总是 returns False
,错误是 This field is required.
但是在我注册了 ReCaptcha 并将 RECAPTCHA_PUBLIC_KEY
和 RECAPTCHA_PRIVATE_KEY
更改为正确的值后,错误消失了并且 form.is_vaild()
returns True
.
我假设您没有为这些配置使用实际值并更改它来解决您的问题。
或者,也许您确实输入了实际值,但仍然出现错误。然后我建议仔细检查这些值。肯定有错别字
我正在尝试在我的一个项目中使用 Recaptcha v3。不幸的是,form.valid_date() 方法总是失败并显示“此字段为必填项”。
运行 django-recaptcha.
forms.py 看起来像这样:
class AccountForm(forms.ModelForm):
# adding some default validators
phone = forms.CharField(validators=[MinLengthValidator(10)], error_messages={'invalid':_("Please add a valid phone number.")})
terms = forms.BooleanField()
captcha = ReCaptchaField(
public_key=settings.RECAPTCHA_PUBLIC_KEY,
private_key=settings.RECAPTCHA_PRIVATE_KEY,
widget=ReCaptchaV3(
attrs={
'required_score': 0.85,
}
)
)
class Meta:
model = Account
fields = ['phone', 'terms']
def clean_phone(self):
data = self.cleaned_data['phone']
if not validate_phone(data):
raise forms.ValidationError(_("Please add a valid phone number. "))
return data
def clean_terms(self):
data = self.cleaned_data['terms']
if not True:
raise forms.ValidationError(_("Please accept the T&Cs"))
return data
views.py 看起来像这样:
def register_web(request: object) -> object:
key = request.GET.get('key', '')
merchant = request.GET.get('merchant', '')
if request.method == 'POST':
print(form.is_valid())
print(form.errors)
if form.is_valid():
phone = phone_filter(str(request.POST.get('phone')).replace(' ', ''))
name = request.POST.get('name', '')
confirmationForm = ConfirmationForm()
return render(request, 'registerWebConfirmation.html', {'context':context })
else:
return render(request, 'registerWeb.html', {'form': form})
else:
return render(request, 'registerWeb.html', {'form': form})
views.py 中的打印状态:
False
<ul class="errorlist"><li>captcha<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
模板如下所示:
<form method="POST" class="post-form">
{% csrf_token %}
{{ form.phone.errors }}
{% render_field form.phone placeholder=form.phone.label class+="form-control" %}
{% if form.name %}
<br /><br />{% render_field form.name placeholder=form.name.label class+="form-control" %}
<input type="hidden" name="advanced" value="1" />
{% endif %}
<br /><br />
{{ form.captcha }}
<input type="hidden" name="merchant" value="{{ merchant }}" />
<input type="hidden" name="key" value="{{ key }}"/>
<div class="d-flex cb-container">
{{ form.terms.errors }}
{% render_field form.terms placeholder=form.terms.label %}
<label for="id_terms">Accept <a href="/dataprivacy">T&Cs</a></label>
</div>
<input class="btnSubmit" type="submit" value="{% trans "Anmelden" %}">
</form>
欢迎提出任何想法。
我试图重现你的问题,正如你所说,表单的 is_valid()
总是 returns False
,错误是 This field is required.
但是在我注册了 ReCaptcha 并将 RECAPTCHA_PUBLIC_KEY
和 RECAPTCHA_PRIVATE_KEY
更改为正确的值后,错误消失了并且 form.is_vaild()
returns True
.
我假设您没有为这些配置使用实际值并更改它来解决您的问题。 或者,也许您确实输入了实际值,但仍然出现错误。然后我建议仔细检查这些值。肯定有错别字