Python 全球名称';注册执行';没有定义

Python 全球名称';注册执行';没有定义,python,django,Python,Django,我犯了一个错误 位于/accounts/regist的名称错误/ 未定义全局名称“RegisterForm” 我确实定义了“RegisterForm”。 我用forms.py写的 from django import forms from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.forms import AuthenticationForm class RegisterForm(Us

我犯了一个错误 位于/accounts/regist的名称错误/ 未定义全局名称“RegisterForm”

我确实定义了“RegisterForm”。 我用forms.py写的

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import AuthenticationForm


class RegisterForm(UserCreationForm):
    def __init__(self, *args, **kwargs):
        __init__(*args, **kwargs)
        self.fields['username'].widget.attrs['class'] = 'form-control'
        self.fields['password1'].widget.attrs['class'] = 'form-control'
        self.fields['password2'].widget.attrs['class'] = 'form-control'


class LoginForm(AuthenticationForm):
    def __init__(self, *args, **kwargs):
        __init__(*args, **kwargs)
        self.fields['username'].widget.attrs['class'] = 'form-control'
        self.fields['password'].widget.attrs['classF'] = 'form-control'
in views.py

from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.views.decorators.http import require_POST



def index(request):
    context = {
        'user': request.user,
    }
    return render(request, 'accounts/index.html', context)


@login_required
def profile(request):
    context = {
        'user': request.user,
    }
    return render(request, 'accounts/profile.html', context)


def regist(request):
    form = RegisterForm(request.POST or None)
    context = {
        'form': form,
    }
    return render(request, 'accounts/regist.html', context)


@require_POST
def regist_save(request):
    form = RegisterForm(request.POST)
    if form.is_valid():
        form.save()
        return redirect('main:index')

    context = {
        'form': form,
    }
    return render(request, 'accounts/regist.html', context)
在URL.py中

from django.conf.urls import url
from . import views
from django.contrib.auth.views import login, logout

urlpatterns = [
    url(r'^login/$', login,
        {'template_name': 'registration/accounts/login.html'},
        name='login'),
    url(r'^logout/$', logout, name='logout'),
    url(r'^regist/$', views.regist,name='regist'),
    url(r'^regist_save/$', views.regist_save, name='regist_save'),
]
我怎样才能修好它?
此外,我真的无法理解我没有在任何地方编写global(我是初学者)

您在forms.py中定义了它,但没有将它导入views.py

另外请注意,您的
\uuuuu init\uuuu
方法将不起作用;这不是调用超类方法的方式。您需要使用
super
方法:

class RegisterForm(UserCreationForm):
    def __init__(self, *args, **kwargs):
        super(RegisterForm, self).__init__(*args, **kwargs)
类似地,LoginForm。

在views.py中添加
from.forms import RegisterForm

thx您的注释。顺便问一下,我应该在哪里编写您的类RegisterForm?是forms.py吗?