我得到:導入失敗,一個奇怪的錯誤
TemplateSyntaxError at /blog/post/test Caught NameError while rendering: global name 'forms' is not defined
此代碼:
forms.py
from dojango.forms import widgets
from django.contrib.comments.forms import CommentForm
from Website.Comments.models import PageComment
class PageCommentForm(CommentForm):
title = widgets.TextInput()
rating = widgets.RatingInput()
def get_comment_model(self):
return PageComment
def get_comment_create_data(self):
# Use the data of the superclass, and add in the title field
data = super(PageComment, self).get_comment_create_data()
data['title'] = self.cleaned_data['title']
return data
models.py
from Website.CMS.models import Author, Rating
from django.db.models import CharField, ForeignKey
from django.contrib.comments.models import Comment
class PageComment(Comment):
title = CharField(max_length=300)
parent = ForeignKey(Author, related_name='parent_id', null=True)
author = ForeignKey(Author, related_name='author_id')
def __unicode__(self):
return self.title
class CommentRating(Rating):
comment = ForeignKey(PageComment)
__init__.py
from Website.Comments import *
def get_model():
return models.PageComment
def get_form():
return forms.PageCommentForm #error here
導入表單裏面直接初始化的.py結果:
AttributeError: 'module' object has no attribute 'Comments'
這裏的堆棧跟蹤,出現錯誤是從dojango來,但並沒有真正有意義:
File "I:\wamp\www\Website\Comments__init__.py", line 1, in from Website.Comments import models, forms File "I:\wamp\www\Website\Comments\forms.py", line 1, in from dojango import forms File "C:\Python26\lib\site-packages\dojango\forms__init__.py", line 2, in from widgets import * File "C:\Python26\lib\site-packages\dojango\forms\widgets.py", line 11, in from dojango.util.config import Config File "C:\Python26\lib\site-packages\dojango\util\config.py", line 3, in from dojango.util import media File "C:\Python26\lib\site-packages\dojango\util\media.py", line 49, in for app in settings.INSTALLED_APPS) File "C:\Python26\lib\site-packages\dojango\util\media.py", line 49, in for app in settings.INSTALLED_APPS) File "C:\Python26\lib\site-packages\dojango\util\media.py", line 38, in find_ pp_dojo_dir_and_url media_dir = find_app_dojo_dir(app_name) File "C:\Python26\lib\site-packages\dojango\util\media.py", line 27, in find_ pp_dojo_dir base = find_app_dir(app_name) File "C:\Python26\lib\site-packages\dojango\util\media.py", line 20, in find_ pp_dir mod = getattr(import(m, {}, {}, [a]), a)
評論應用程序在安裝的應用程序中。 我該怎麼辦?
編輯: 如果我嘗試包括形式直接與import forms
我得到這個:
Traceback (most recent call last): File "I:\wamp\www\Website\manage.py", line 11, in execute_manager(settings)
File "C:\Python26\lib\site-packages\django\core\management__init__.py", line 438, in execute_manager utility.execute()
File "C:\Python26\lib\site-packages\django\core\management__init__.py", line 379, in execute self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python26\lib\site-packages\django\core\management\base.py", line 191, in run_from_argv self.execute(*args, **options.dict)
File "C:\Python26\lib\site-packages\django\core\management\base.py", line 209, in execute translation.activate('en-us')
File "C:\Python26\lib\site-packages\django\utils\translation__init__.py", lin e 66, in activate return real_activate(language)
File "C:\Python26\lib\site-packages\django\utils\functional.py", line 55, in _ curried return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
File "C:\Python26\lib\site-packages\django\utils\translation__init__.py", lin e 36, in delayed_loader return getattr(trans, real_name)(*args, **kwargs)
File "C:\Python26\lib\site-packages\django\utils\translation\trans_real.py", l ine 193, in activate _active[currentThread()] = translation(language)
File "C:\Python26\lib\site-packages\django\utils\translation\trans_real.py", l ine 176, in translation default_translation = _fetch(settings.LANGUAGE_CODE)
File "C:\Python26\lib\site-packages\django\utils\translation\trans_real.py", l ine 159, in _fetch app = import_module(appname)
File "C:\Python26\lib\site-packages\django\utils\importlib.py", line 35, in im port_module import(name)
File "I:\wamp\www\Website\Comments__init__.py", line 2, in import forms
File "I:\wamp\www\Website\Comments\forms.py", line 3, in from dojango.forms import fields, widgets
File "C:\Python26\lib\site-packages\dojango\forms__init__.py", line 2, in from widgets import *
File "C:\Python26\lib\site-packages\dojango\forms\widgets.py", line 11, in from dojango.util.config import Config
File "C:\Python26\lib\site-packages\dojango\util\config.py", line 3, in from dojango.util import media
File "C:\Python26\lib\site-packages\dojango\util\media.py", line 49, in for app in settings.INSTALLED_APPS)
File "C:\Python26\lib\site-packages\dojango\util\media.py", line 49, in for app in settings.INSTALLED_APPS)
File "C:\Python26\lib\site-packages\dojango\util\media.py", line 38, in find_a pp_dojo_dir_and_url media_dir = find_app_dojo_dir(app_name)
File "C:\Python26\lib\site-packages\dojango\util\media.py", line 27, in find_a pp_dojo_dir base = find_app_dir(app_name)
File "C:\Python26\lib\site-packages\dojango\util\media.py", line 20, in find_a pp_dir mod = getattr(import(m, {}, {}, [a]), a)
AttributeError: 'module' object has no attribute 'Comments'
刪除了dojango任何參考解決問題。
因爲它在裏面__init__.py我不能指定完整的模塊路徑?但爲什麼從dojango.forms中導入小部件'和所有dojango引用都有幫助?是否因爲dojango也有一個表單應用程序?我很困惑。你能告訴我爲什麼會發生這個錯誤嗎? – 2010-08-08 06:20:48
是的,我相信有一個django.forms。你可能想嘗試:從my_project導入表格 其中my_project是你所在的根目錄的名稱,我認爲*網站*在你的情況。 – 2010-08-08 16:58:50
當我從Website.Comments導入表單的時候,我也收到了一個錯誤,請看詳細問題 – 2010-08-08 18:01:35