我下面DjangoBook教程和我所遇到的第14章(用戶註冊)問題登記表導入錯誤
在django.contrib.auth.forms,有UserCreationForm
類。我想在django.contrib.auth.forms
class RegisterForm(UserCreationForm):
email = forms.EmailField(label="Email")
fullname = forms.CharField(label="Full name")
class Meta:
model= User
fields = ("username","fullname","email",)
創建一個名爲RegisterForm
這是我RegisterForm
類基於UserCreationForm
一個新的類這是視圖功能
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
new_user = form.save()
return HttpResponseRedirect("/books/")
else:
form = RegisterForm(UserCreationForm)
return render(request, "registration/register.html", {
'form': form,
})
我不知道爲什麼我得到一個ImportError,其中指出我無法在我的視圖中導入名稱RegisterForm
,有人可以幫我解答。
這是我的堆棧跟蹤
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/register/
Django Version: 1.5.1
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.staticfiles',
'django.contrib.admin',
'mysite.books')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
103. resolver_match = resolver.resolve(request.path_info)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
319. for pattern in self.url_patterns:
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in url_patterns
347. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
342. self._urlconf_module = import_module(self.urlconf_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
35. __import__(name)
File "C:\Users\Ray Lim\Desktop\project\home\username\djcode\mysite\mysite\urls.py" in <module>
14. from mysite.books import views
File "C:\Users\Ray Lim\Desktop\project\home\username\djcode\mysite\mysite\books\views.py" in <module>
11. from django.contrib.auth.forms import RegisterForm
Exception Type: ImportError at /register/
Exception Value: cannot import name RegisterForm
你編輯過'django.contrib.auth.forms'來添加你的類嗎?什麼是完整的堆棧跟蹤? – Rohan 2013-05-02 06:57:43
我很困惑 - UserCreationForm是RegisterForm的父級?爲什麼然後你發送視圖函數作爲參數('form = RegisterForm(UserCreationForm)')? – 2013-05-02 06:58:11
@DenisMalinovsky我是Django的新手,我應該如何調用RegisterForm。 Rohan:是的,我已經添加了類到django.contrib.auth.forms – user2284926 2013-05-02 07:13:15