2013-05-02 79 views
0

我下面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 
+0

你編輯過'django.contrib.auth.forms'來添加你的類嗎?什麼是完整的堆棧跟蹤? – Rohan 2013-05-02 06:57:43

+0

我很困惑 - UserCreationForm是RegisterForm的父級?爲什麼然後你發送視圖函數作爲參數('form = RegisterForm(UserCreationForm)')? – 2013-05-02 06:58:11

+0

@DenisMalinovsky我是Django的新手,我應該如何調用RegisterForm。 Rohan:是的,我已經添加了類到django.contrib.auth.forms – user2284926 2013-05-02 07:13:15

回答

1

由於的Django 1.5有模塊django.contrib.auth.forms.

你可能想使用要強調這樣的事情對你的編輯器中沒有這樣的形式。如PyCharm

儘管如此,點支架,您應該至少對Python瞭解Django的基本瞭解。你看,這些陳述

class RegisterForm(UserCreationForm)  
    ... 
# And then: 
form = RegisterForm(UserCreationForm) 

很難從Python的角度來理解。

您正在編寫Python代碼。在Python中。你必須學習Python。是的,有可能以另一種方式行事,但這很難。

+0

我在開始時導入了RegisterForm – user2284926 2013-05-02 07:26:44