1

我正在尝试按照本教程http://blog.bixly.com/post/4069885657/how-to-add-recaptcha-to-your-django-forms将 google reCAPTCHA 插件实施到我的表单中

台阶向前看

Here are the steps to make it work:

1.Obtain a public and a private key.
2.Download a plugin for python.
3.Render the widget in the form page.
4.Verify the answer by sending a request to google’s server.
5.Check the response.

但是当我在表单页面中渲染小部件的第 3 步时。我得到这个错误。通过阅读教程,它并没有让我导入任何东西。有人能帮我吗

渲染小部件 您需要的插件模块位于 recaptcha.client.captcha 中。它包含一个名为 displayhtml() 的方法,该方法返回一个用于呈现小部件的 javascript 代码字符串。这是一个示例用法:

 NameError at /account/forgot-password/

 global name 'displayhtml' is not defined
 Request Method:    GET
 Request URL:   http://127.0.0.1:8000/account/forgot-password/
 Django Version:    1.4.3
 Exception Type:    NameError
 Exception Value:   

 global name 'displayhtml' is not defined

 Traceback:
 File "C:\Python26\lib\site-packages\django\core\handlers\base.py" in get_response
   111.                         response = callback(request, *callback_args, **callback_kwargs)
 File "C:\o\17\mysite\accounts\views.py" in forgot_password
   12.      script = displayhtml(public_key=public_key)

 Exception Type: NameError at /account/forgot-password/
 Exception Value: global name 'displayhtml' is not defined

以下是使其工作的步骤:

Obtain a public and a private key.
Download a plugin for python.
Render the widget in the form page.
Verify the answer by sending a request to google’s server.
Check the response.

我的意见.py

from django.contrib.auth.views import password_reset
from django.shortcuts import render
from mysite.settings import *

def forgot_password(request):
    if request.method == 'POST':
        return password_reset(request,
            from_email=request.POST.get('email'))

    else:
        public_key = 'DAAW231rf2ef23rfq'
        script = displayhtml(public_key=public_key)
        return render(request, 'forgot_password.html',{'script':script})

模板

  <form>
    {{ form }}{% csrf_token %}
    {{ script }}
</form>
4

1 回答 1

2

您需要从获取displayhtml()函数的位置导入模块。

from somemodule import displayhtml
于 2013-05-25T14:47:31.593 回答