django-nocaptcha-recaptcha始终显示其他验证框

时间:2015-04-01 17:17:07

标签: django python-3.x recaptcha

我安装了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。我正在使用django 1.7.1和python 3.4

2 个答案:

答案 0 :(得分: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'])

答案 1 :(得分:0)

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