未定义全局名称“displayhtml”

时间:2013-05-25 14:45:23

标签: django

我正在尝试按照本教程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.

my views.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>

1 个答案:

答案 0 :(得分:2)

您需要从中获取displayhtml()功能的模块。

from somemodule import displayhtml