django-nocaptcha-recaptcha 总是显示额外的验证框

django-nocaptcha-recaptcha always shows additional verification box

我安装了 django-nocaptcha-recaptcha 并将其集成到我的表单中:

from nocaptcha_recaptcha.fields import NoReCaptchaField

class ClientForm(forms.ModelForm):
    captcha = NoReCaptchaField()

它在表单上显示正常,但每当我单击它时,都会弹出一个额外的对话框,要求输入一些文本并进行验证。它每次都会发生。我在另一个网络上的另一台计算机上对其进行了测试,但在单击该框后它仍然要求进行额外的验证。

这是它的样子:additional verification dialog box

这是我处理表单的方式:

@xframe_options_exempt
def registration(request):           
    if request.method == 'POST':        
        clientform = ClientForm(request.POST)

    # check whether it's valid:
    if clientform.is_valid():
        new_client = clientform.save()
...

我做错了什么?是 django-nocaptcha-recaptcha 的问题吗?我应该使用其他东西吗?

P.S。我在 python 3.4

中使用 django 1.7.1

重新启动服务器,不要忘记清除浏览器的缓存。希望这有帮助。

Another alternative:极简主义且不依赖于框架。 这是代码,如果你想重写它。

 '''
    NO-CAPTCHA VERSION: 1.0
    PYTHON VERSION:     3.x
 '''

import json
from urllib.request import Request, urlopen
from urllib.parse import urlencode


VERIFY_SERVER = "www.google.com"


class RecaptchaResponse(object):

    def __init__(self, is_valid, error_code=None):
        self.is_valid = is_valid
        self.error_code = error_code

    def __repr__(self):
        return "Recaptcha response: %s %s" % (
            self.is_valid, self.error_code)

    def __str__(self):
        return self.__repr__()


def displayhtml(site_key, language=''):
    """Gets the HTML to display for reCAPTCHA

    site_key -- The site key
    language -- The language code for the widget.
    """

    return """<script src="https://www.google.com/recaptcha/api.js?hl=%(LanguageCode)s" async="async" defer="defer"></script>
  <div class="g-recaptcha" data-sitekey="%(SiteKey)s"></div>
""" % {
        'LanguageCode': language,
        'SiteKey': site_key,
    }


def submit(response,
           secret_key,
           remote_ip,
           verify_server=VERIFY_SERVER):
    """
    Submits a reCAPTCHA request for verification. Returns RecaptchaResponse
    for the request

    response -- The value of response from the form
    secret_key -- your reCAPTCHA secret key
    remote_ip -- the user's ip address
    """

    if not(response and len(response)):
        return RecaptchaResponse(is_valid=False, error_code='incorrect-captcha-sol')

    def encode_if_necessary(s):
        if isinstance(s, str):
            return s.encode('utf-8')
        return s

    params = urlencode({
        'secret': encode_if_necessary(secret_key),
        'remoteip': encode_if_necessary(remote_ip),
        'response': encode_if_necessary(response),
    })

    params = params.encode('utf-8')

    request = Request(
        url="https://%s/recaptcha/api/siteverify" % verify_server,
        data=params,
        headers={
            "Content-type": "application/x-www-form-urlencoded",
            "User-agent": "reCAPTCHA Python"
        }
    )
    httpresp = urlopen(request)

    return_values = json.loads(httpresp.read().decode('utf-8'))
    httpresp.close()

    return_code = return_values['success']

    if return_code:
        return RecaptchaResponse(is_valid=True)
    else:
        return RecaptchaResponse(is_valid=False, error_code=return_values['error-codes'])