如何将reCAPTCHA V3集成到Django中的HTML表单中?

pnwntuvh  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(118)

我遵循所有可能的教程,如谷歌,我使后端,之后,我移动到前面(这是在同一个项目)。
当然,在后端,我会在设置中设置我的密钥私钥和公钥。
settings.py

RECAPTCHA_PUBLIC_KEY = "secret"
RECAPTCHA_PRIVATE_KEY = "secreter"

字符串
这是我的users.view.py和设置表单字段的Class:

class createNewAccountForm(forms.Form):
    name = forms.CharField(required=True, validators=[validate_alphanumericplus])
    email = forms.CharField(required=True)
    password = SetPasswordField(label='Password', required=True)
    captcha = ReCaptchaField(
            public_key=settings.RECAPTCHA_PUBLIC_KEY,
            private_key=settings.RECAPTCHA_PRIVATE_KEY,
            widget=ReCaptchaV3(
                attrs={
                    'required_score': 0.85,
                }
            )
        )


这是我设置HTML并使注册发生的类:

class createNewAccountView(FormView):
    form_class = createNewAccountForm
    template_name = 'account/create.html'

    def form_valid(self, form):

        captcha_score = form.cleaned_data['captcha'].verify(self.request)
        if captcha_score < 0.5:
            form.add_error(None, 'reCAPTCHA verification failed.')
            return self.form_invalid

        print("form is valid, creating user")
        print(form.cleaned_data)
        name = form.cleaned_data['name']
        email = form.cleaned_data['email']
        password = form.cleaned_data['password']
        next = form.data.get('next', None)


这是create.html表单的一部分:

<form method="post" action="{% url 'users:create_account' %}" id="create-form">
          {% csrf_token %}

            {% if request.GET.next %}
              <input id="next" name="next" type="hidden" value="{{ request.GET.next }}">
            {% endif %}

            <div>
              <label for="support_full_name">
                Name
              </label>
              <div>
                <input id="support_full_name" name="name" type="text" required aria-required="true" placeholder="Your name" class="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
              </div>
            </div>

            <div>
              <label for="email" id="email_label">
                    Email address
              </label>

              <div>
                <input id="email" name="email" type="email" autocomplete="email" placeholder="Your work email address" required aria-required="true">
              </div>
            </div>

            <div>
                            # Trying to put the captcha field here!
              {{ form.catpcha }}
            </div>

            <div>
              <label for="password">
                  Choose a password
                </label>
                <div>
                  <input id="password" name="password" type="password" autocomplete="current-password" required aria-required="true" placeholder="Password" class="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                </div>
            </div>

            <div>
                <button type="submit">
                  Get started
                </button>
            </div>

          </form>


我也尝试过与JavaScript集成,但这一点在Django Templates中并不清楚。
https://developers.google.com/recaptcha/docs/v3
我的前端有什么问题?还是跟后台有关?

uz75evzq

uz75evzq1#

试试这个为什么
reptcha_response = request.POST.get('g-reptcha-response ')

data = {
            'secret': '',
            'response': recaptcha_response
        }

    r = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data)

    result = r.json()
    print(result)

    try:
        if result['success']:
            send_mail(subject, stripped_template, '', [''],fail_silently=False)
            messages.success(request, 'Your Message has been Sent Successfully.')

    except BadHeaderError:
        return HttpResponse('Invalid header found.')

字符串

相关问题