title: Django Captcha Decorator
body: <p>I've used a reCaptcha system created by the <a href="https://simpleisbetterthancomplex.com/tutorial/2017/02/21/how-to-add-recaptcha-to-django-site.html" rel="nofollow noreferrer">simpleisbetterthancomplex.com/</a> website.</p>
  
  <p>In class-based views, it works perfectly fine, but whenever it comes to built-in libraries like:</p>
  
  <pre><code>from django.contrib.auth import views as auth_views
  </code></pre>
  
  <p>and functionalities:</p>
  
  <pre><code>auth_views.password_reset
  </code></pre>
  
  <p>it literally does not work as it should. The process of confirming the code of captcha is not executed at all.
  I'm having a following usage of it:</p>
  
  <pre><code>url(r'^password/reset/$', check_recaptcha(auth_views.password_reset), name='password_reset'),
  </code></pre>
  
  <p><strong>decorators.py:</strong></p>
  
  <pre><code>def check_recaptcha(view_func):
      @wraps(view_func)
      def _wrapped_view(request, *args, **kwargs):
          request.recaptcha_is_valid = None
          if request.method == 'POST':
              recaptcha_response = request.POST.get('g-recaptcha-response')
              url = 'https://www.google.com/recaptcha/api/siteverify'
              values = {
                  'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY,
                  'response': recaptcha_response
              }
              data = urllib.urlencode(values)
              req = urllib2.Request(url, data)
              response = urllib2.urlopen(req)
              result = json.load(response)
              if result['success']:
                  request.recaptcha_is_valid = True
              else:
                  request.recaptcha_is_valid = False
                  messages.error(request, 'Invalid reCAPTCHA. Please try again.')
                  print(str('Invalid reCAPTCHA. Please try again.'))
          return view_func(request, *args, **kwargs)
      return _wrapped_view
  </code></pre>
  
  <p><strong>password_reset_template.html</strong></p>
  
  <pre><code>{% block content %}
    &lt;div class="row justify-content-center"&gt;
      &lt;div class="col-lg-4 col-md-4 col-sm-4"&gt;
      &lt;/div&gt;
      &lt;div class="col-lg-4 col-md-4 col-sm-4"&gt;
        &lt;div class="row main"&gt;
            &lt;div class="main-login main-center"&gt;
              &lt;center&gt;
              &lt;br&gt;
              &lt;h3 class="card-title"&gt;&lt;b&gt;E-mail:&lt;/b&gt;&lt;/h3&gt;&lt;br&gt;
              &lt;form method="post" action="."&gt;
                {% csrf_token %}
                  {% for field in form %}
                      &lt;div class="form-group" style="padding: 0px 40px;"&gt;
                        {% if field.errors %}
                          &lt;div class="alert alert-danger"&gt;
                            {{ field.errors }}
                          &lt;/div&gt;
                          {% endif %}
                          &lt;div class="cols-sm-10"&gt;
                            &lt;div class="input-group"&gt;
                              &lt;span class="input-group-addon"&gt;&lt;i class="fa fa-envelope" aria-hidden="true"&gt;&lt;/i&gt;&lt;/span&gt;
                              {{ field }}
                            &lt;/div&gt;
                          &lt;/div&gt;
                          {% if field.help_text %}
                          &lt;p class="help"&gt;{{ field.help_text|safe }}&lt;/p&gt;
                          {% endif %}
                      &lt;/div&gt;
                  {% endfor %}
                  &lt;br&gt;
                  &lt;div style="padding: 0px 40px;"&gt;
                    &lt;button type="submit" class="btn btn-primary btn-block"&gt;Reset Password&lt;/button&gt;
                  &lt;/div&gt;
                  &lt;br&gt;
                  &lt;div class="g-recaptcha"  data-sitekey="xxxxxx"&gt;&lt;/div&gt;
                  &lt;br&gt;
              &lt;/form&gt;
              &lt;/center&gt;
            &lt;/div&gt;
            &lt;br&gt;
        &lt;/div&gt;
        &lt;div class="col-lg-4 col-md-4 col-sm-4"&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  {% endblock %}
  </code></pre>