我得到:
/blog/post/test 处的 TemplateSyntaxError 在渲染时捕获 NameError:未定义全局名称“forms”
对于此代码:
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
模型.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
直接在init .py 中导入表单会导致:
AttributeError:“模块”对象没有属性“评论”
这是堆栈跟踪,错误似乎来自 dojango 但这并没有真正意义:
文件“I:\wamp\www\Website\Comments__init__.py”,第 1 行,来自 Website.Comments 导入模型,表格文件“I:\wamp\www\Website\Comments\forms.py”,第 1 行,在从 dojango 导入表单文件“C:\Python26\lib\site-packages\dojango\forms__init__.py”,第 2 行,从小部件导入 * 文件“C:\Python26\lib\site-packages\dojango\forms\widgets .py”,第 11 行,从 dojango.util.config 导入配置文件“C:\Python26\lib\site-packages\dojango\util\config.py”,第 3 行,从 dojango.util 导入媒体文件“ C:\Python26\lib\site-packages\dojango\util\media.py”,第 49 行,在 settings.INSTALLED_APPS 中的应用程序中)文件“C:\Python26\lib\site-packages\dojango\util\media. py",第 49 行,用于设置中的应用程序。INSTALLED_APPS) 文件“C:\Python26\lib\site-packages\dojango\util\media.py”,第 38 行,在 find_pp_dojo_dir_and_url media_dir = find_app_dojo_dir(app_name) 文件“C:\Python26\lib\site-packages\dojango \util\media.py",第 27 行,在 find_pp_dojo_dir base = find_app_dir(app_name) 文件 "C:\Python26\lib\site-packages\dojango\util\media.py",第 20 行,在 find_pp_dir mod =获取属性(在 find_pp_dir mod = getattr(在 find_pp_dir mod = getattr(导入(m, {}, {}, [a]), a)
评论应用程序位于已安装的应用程序中。我应该怎么办?
编辑:如果我尝试直接包含表单,import forms
我会得到:
回溯(最后一次调用):文件“I:\wamp\www\Website\manage.py”,第 11 行,在 execute_manager(settings)
文件“C:\Python26\lib\site-packages\django\core\management__init__ .py”,第 438 行,在 execute_manager utility.execute()
文件“C:\Python26\lib\site-packages\django\core\management__init__.py”,第 379 行,在执行 self.fetch_command(subcommand).run_from_argv( self.argv)
文件“C:\Python26\lib\site-packages\django\core\management\base.py”,第 191 行,在 run_from_argv self.execute(*args, **options.dict )文件
“C: \Python26\lib\site-packages\django\core\management\base.py",第 209 行,在执行 translation.activate('en-us')
文件“C:\Python26\lib\site-packages\django\utils\translation__init__.py”,第 66 行,激活返回 real_activate(language)
文件“C:\Python26\lib\site-packages\django\utils\ functional.py”,第 55 行,在 _ curried return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
文件“C:\Python26\lib\site-packages\django\utils\translation__init__ .py”,第 36 行,在延迟加载程序中返回 getattr(trans, real_name)(*args, **kwargs)
文件“C:\Python26\lib\site-packages\django\utils\translation\trans_real.py”,l ine 193,在激活 _active[currentThread()] = translation(language)
文件“C:\Python26\lib\site-packages\django\utils\translation\trans_real.py”,第 176 行,翻译中 default_translation = _fetch(settings.LANGUAGE_CODE)
文件“C:\Python26\lib\site-packages \django\utils\translation\trans_real.py”,第 159 行,在 _fetch app = import_module(appname)
文件“C:\Python26\lib\site-packages\django\utils\importlib.py”,第 35 行,在im port_module import (name)
File "I:\wamp\www\Website\Comments__init__.py", line 2, in import forms
文件 "I:\wamp\www\Website\Comments\forms.py", line 3, in从 dojango.forms 导入字段,小部件
文件“C:\Python26\lib\site-packages\dojango\forms__init__.py”,第 2 行,从小部件导入 *
文件“C:\Python26\lib\site-packages\dojango\forms\widgets.py”,第 11 行,从 dojango.util.config 导入配置
文件“C:\Python26\lib\site-packages\dojango\util \config.py",第 3 行,从 dojango.util 导入媒体
文件 "C:\Python26\lib\site-packages\dojango\util\media.py",第 49 行,在 settings.INSTALLED_APPS 中的应用程序
文件中“C:\Python26\lib\site-packages\dojango\util\media.py”,第 49 行,在 settings.INSTALLED_APPS 中的应用程序中)
文件“C:\Python26\lib\site-packages\dojango\util\media .py”,第 38 行,在 find_a pp_dojo_dir_and_url media_dir = find_app_dojo_dir(app_name)
文件“C:\Python26\lib\site-packages\dojango\util\media.py”中,第 27 行,在 find_a pp_dojo_dir base = find_app_dir(app_name)
文件“C:\Python26\lib\site-packages\dojango\util\media.py”,第 20 行,在 find_a pp_dir mod = getattr( import (m, {}, {}, [a]), a)
AttributeError :“模块”对象没有属性“评论”
删除对 dojango 的任何引用可以解决问题。