未定义全局名称“RegistrationForm”

时间:2012-04-04 09:20:59

标签: python django

def register_page(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.creat_user(
                username=form.clean_data['username'],
                password=form.clean_data['password1'],
                email=form.clean_data['email']
                )
            return HttpResponseRedirect('/accounts/register/success/')
    else:
        form = RegistrationForm()
    variables = RequestContext(request, {'form': form})
    return render_to_response ('registration/register.html',variables)

当我运行服务器时,会出现此错误:

global name 'RegistrationForm' is not defined
Request Method: GET
Request URL:    http://127.0.0.1:8000/accounts/register/
Django Version: 1.4
Exception Type: NameError
Exception Value:    
global name 'RegistrationForm' is not defined
Exception Location: D:\Python\Scripts\littlesite\messageboard\views.py in register_page, line 31
Python Executable:  D:\Python\python.exe
Python Version: 2.7.2

有人可以告诉我它有什么问题吗?感谢。

1 个答案:

答案 0 :(得分:1)

您没有导入RegistrationForm。要使用它,您需要将其导入当前的命名空间。

如果您使用django-registration,请使用以下导入:

from registration.forms import RegistrationForm

如果是自定义表单,请从您定义的位置导入它。