title: Best way to use google recaptcha V3 in django
body: <p>First I should say that I've read <a href="https://github.com/kbytesys/django-recaptcha3" rel="nofollow noreferrer">this</a>, and It is not what I need. I want to have forms that I made with html/css not django forms.  </p>
  
  <p>And my method is based on <a href="https://codeforgeek.com/2019/02/google-recaptcha-v3-tutorial/" rel="nofollow noreferrer">this</a>, that is actually in PHP.</p>
  
  <p><strong><em>I wrote code and it works fine</em></strong>, <em>but I believe there should be a better way to do it.</em></p>
  
  <p>The summery of code is that I submit my form, post the data and google recaptcha token to my function, and then do some process on it, then to redirect to the relative page base on results of processes, I return the url and status to jQuery again, and redirect to that page using jQuery.  </p>
  
  <p>Here is my code: </p>
  
  <p><strong>login.html:</strong></p>
  
  <pre><code>&lt;script src="https://www.google.com/recaptcha/api.js?render=here is recaptcha public key"&gt;&lt;/script&gt;
  &lt;!-- login form and etc --&gt;
  
  &lt;script&gt;
  $('#loginForm').submit(function() {
      // stop what loginform is going to do
      event.preventDefault();
      var username = $('#username').val();
      var password = $("#password").val();
      grecaptcha.ready(function () {
        grecaptcha.execute("here is recaptcha public key", 
           {action: "{% url 'basic_app:login_page' %}"}).then(function (token_id) 
                {$('#loginForm').prepend('&lt;input type="hidden" name="g-recaptcha-response" value="' + token_id + '"&gt;');
                  $.post("{% url 'basic_app:login' %}",  // url
                     {username: username,password: password,
                      token_id: token_id,csrfmiddlewaretoken: '{{ csrf_token }}'},
                         function( result ){
                              console.log(result);
                              if(result.status == 0) {
                                      window.location.replace(result.url)
                              } else if (result.status == 1){
                                      window.location.replace(result.url)
                              }
                          },
                  'json');
            });
        });
    });
  </code></pre>
  
  <p></p>
  
  <p><strong>views.py:</strong></p>
  
  <pre><code>def user_login(request):
      if request.method == "POST":
          username = request.POST.get('username')
          password = request.POST.get('password')
          token_id = request.POST.get('token_id')
          if(token_id):
              secretKey = "here is recaptcha secret key"
              data = {
                  'secret': secretKey,
                  'response': token
              }
              r = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data)
              result = r.json()
              ....
              ## some codes for more security 
              ....
              response = {'status': 0, 'message':"", 'url': '/'}
              return HttpResponse(json.dumps(response), content_type='application/json')
          else:
              response = {'status': 0, 'message':"", 'url': '/login_page'}
              return HttpResponse(json.dumps(response), content_type='application/json')     
  
  ....
  </code></pre>
  
  <p>is there any security problem in this method ?<br>
  and is there any way to write a better code to use recaptcha V3 ?
  thank you.</p>