Django Captcha装饰

时间:2018-04-16 19:40:04

标签: django recaptcha

我使用了simpleisbetterthancomplex.com/网站创建的reCaptcha系统。

在基于类的视图中,它可以很好地工作,但无论何时使用内置库,如:

from django.contrib.auth import views as auth_views

和功能:

auth_views.password_reset

它确实无法正常工作。确认验证码的代码的过程根本没有执行。 我有以下用法:

url(r'^password/reset/$', check_recaptcha(auth_views.password_reset), name='password_reset'),

decorators.py:

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

password_reset_template.html

{% block content %}
  <div class="row justify-content-center">
    <div class="col-lg-4 col-md-4 col-sm-4">
    </div>
    <div class="col-lg-4 col-md-4 col-sm-4">
      <div class="row main">
          <div class="main-login main-center">
            <center>
            <br>
            <h3 class="card-title"><b>E-mail:</b></h3><br>
            <form method="post" action=".">
              {% csrf_token %}
                {% for field in form %}
                    <div class="form-group" style="padding: 0px 40px;">
                      {% if field.errors %}
                        <div class="alert alert-danger">
                          {{ field.errors }}
                        </div>
                        {% endif %}
                        <div class="cols-sm-10">
                          <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-envelope" aria-hidden="true"></i></span>
                            {{ field }}
                          </div>
                        </div>
                        {% if field.help_text %}
                        <p class="help">{{ field.help_text|safe }}</p>
                        {% endif %}
                    </div>
                {% endfor %}
                <br>
                <div style="padding: 0px 40px;">
                  <button type="submit" class="btn btn-primary btn-block">Reset Password</button>
                </div>
                <br>
                <div class="g-recaptcha"  data-sitekey="xxxxxx"></div>
                <br>
            </form>
            </center>
          </div>
          <br>
      </div>
      <div class="col-lg-4 col-md-4 col-sm-4">
      </div>
    </div>
  </div>
{% endblock %}

0 个答案:

没有答案