1

I'm working with a project using Python(3.7) and Django(2) in which I have to implement Google's Recaptcha. For authentication I'm using the built-in LoginView from Django,contrib.auth.views, How can I validate the captcha by utilising this auth view? Because in our custom cases, we send a request to Recaptcha api by passing the keys and check if the result is Success, but in this we are passing it to this built-in view.

Here's what I have tried so far:

From urls.py:

path('login/', LoginView.as_view(extra_context={'key': settings.RECAPTCHA_PUBLIC_KEY}), name='login'),

From login.html:

<form action="{% url 'login' %}" method="post" class="p-3">
                    {% csrf_token %}
    <div class="form-group">
         <label for="recipient-name" class="col-form-label">Username</label>
         <input type="text" class="form-control" placeholder=" " name="username" id="recipient-name"
                               required="">
     </div>
     <div class="form-group">
         <label for="password" class="col-form-label">Password</label>
         <input type="password" class="form-control" placeholder=" " name="password" id="password"
                               required="">
     </div>
     <div class="form-group">
          <script src='https://www.google.com/recaptcha/api.js'></script>
          <div class="g-recaptcha" data-sitekey="{{ key }}"> {{ key }}</div>
     </div>
     <div class="right-w3l">
          <input type="submit" class="form-control" value="Login">
     </div>

</form>
3
  • You can create a custom back end and extend the LoginView to your CustomLoginView. In your custom back end you can validate the key success input passed from the client side.
    – Prakash S
    Jul 13, 2019 at 7:47
  • which method do I need to override? where I can validate the Recaptcha and save the object or whatever I need to do after verification? Jul 13, 2019 at 12:00
  • See this post: stackoverflow.com/a/54921526
    – Prakash S
    Jul 14, 2019 at 7:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.