I am trying to use Recaptcha v3 in one of my projects. Unfortunately the form.valid_date() method fails all the time with "This field is required.".
Running django-recaptcha.
forms.py looks like this:
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 looks like this:
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})
The print in the views.py states:
False
<ul class="errorlist"><li>captcha<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
The template looks like this:
<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>
Any ideas are welcome.
I tried to replicate your problem and as you said the form's is_valid()
always returns False
with the error that This field is required.
But after I signed up for a ReCaptcha and change the RECAPTCHA_PUBLIC_KEY
and RECAPTCHA_PRIVATE_KEY
to proper values, the error disappeared and the form.is_vaild()
returns True
.
I assume you're not using real values for these configs and changing that fixes your problem. Or maybe you do put the real values but you're still getting the error. Then I suggest double checking the values. There must be a typo.