导入失败,出现奇怪错误

0 投票
2 回答
750 浏览
提问于 2025-04-16 02:29

我遇到了这个问题:

在 /blog/post/test 处出现了模板语法错误 渲染时捕获到名称错误: 全局名称 '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

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

init.py中直接导入表单会导致:

属性错误:'模块'对象没有属性 'Comments'

这是错误的堆栈跟踪,错误似乎来自 dojango,但这并不太合理:

文件 "I:\wamp\www\Website\Comments__init__.py", 第1行,来自 from Website.Comments import models, forms 文件 "I:\wamp\www\Website\Comments\forms.py", 第1行,来自 from dojango import forms 文件 "C:\Python26\lib\site-packages\dojango\forms__init__.py", 第2行,来自 from widgets import * 文件 "C:\Python26\lib\site-packages\dojango\forms\widgets.py", 第11行,来自 from dojango.util.config import Config 文件 "C:\Python26\lib\site-packages\dojango\util\config.py", 第3行,来自 from dojango.util import media 文件 "C:\Python26\lib\site-packages\dojango\util\media.py", 第49行,来自 for app in settings.INSTALLED_APPS) 文件 "C:\Python26\lib\site-packages\dojango\util\media.py", 第49行,来自 for app in settings.INSTALLED_APPS) 文件 "C:\Python26\lib\site-packages\dojango\util\media.py", 第38行,来自 find_app_dojo_dir_and_url media_dir = find_app_dojo_dir(app_name) 文件 "C:\Python26\lib\site-packages\dojango\util\media.py", 第27行,来自 find_app_dir base = find_app_dir(app_name) 文件 "C:\Python26\lib\site-packages\dojango\util\media.py", 第20行,来自 find_app_dir mod = getattr(import(m, {}, {}, [a]), a)

Comments 应用已经在已安装的应用中。 我该怎么办?

编辑: 如果我尝试直接用 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行,来自 execute 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行, 在 execute translation.activate('en-us')
文件 "C:\Python26\lib\site-packages\django\utils\translation__init__.py",第66行,来自 activate return 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行,来自 delayed_loader return getattr(trans, real_name)(*args, **kwargs)
文件 "C:\Python26\lib\site-packages\django\utils\translation\trans_real.py",第193行,来自 activate _active[currentThread()] = translation(language)
文件 "C:\Python26\lib\site-packages\django\utils\translation\trans_real.py",第176行,来自 translation 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行,来自 import_module import(name)
文件 "I:\wamp\www\Website\Comments__init__.py",第2行,来自 import forms
文件 "I:\wamp\www\Website\Comments\forms.py",第3行,来自 from dojango.forms import fields, widgets
文件 "C:\Python26\lib\site-packages\dojango\forms__init__.py",第2行,来自 from widgets import *
文件 "C:\Python26\lib\site-packages\dojango\forms\widgets.py",第11行,来自 from dojango.util.config import Config
文件 "C:\Python26\lib\site-packages\dojango\util\config.py",第3行,来自 from dojango.util import media
文件 "C:\Python26\lib\site-packages\dojango\util\media.py",第49行,来自 for app in settings.INSTALLED_APPS)
文件 "C:\Python26\lib\site-packages\dojango\util\media.py",第49行,来自 for app in settings.INSTALLED_APPS)
文件 "C:\Python26\lib\site-packages\dojango\util\media.py",第38行,来自 find_app_dojo_dir_and_url media_dir = find_app_dojo_dir(app_name)
文件 "C:\Python26\lib\site-packages\dojango\util\media.py",第27行,来自 find_app_dir base = find_app_dir(app_name)
文件 "C:\Python26\lib\site-packages\dojango\util\media.py",第20行,来自 find_app_dir mod = getattr(import(m, {}, {}, [a]), a)
属性错误:'模块'对象没有属性 'Comments'

去掉任何对 dojango 的引用就解决了这个问题。

2 个回答

0

在 __init__.py 文件中放入以下内容:

import forms
0

这是dojango里的一个错误。
我会去报告这个问题。

撰写回答