导入失败并出现奇怪的错误

发布于 2024-09-13 04:43:09 字数 5694 浏览 2 评论 0原文

我得到:

/blog/post/test 处的模板语法错误 渲染时捕获 NameError: 全局名称“forms”未定义

为此代码定义全局名称“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 中导入表单会导致:

属性错误:“模块”对象没有 属性“评论”

这是堆栈跟踪,错误似乎来自 dojango,但这实际上没有意义:

文件 “我:\wamp\www\网站\Comments__init__.py”, 第 1 行,在 从网站.评论导入模型、表格文件 “我:\wamp\www\网站\评论\forms.py”, 第 1 行,在 从 dojango 导入表单文件“C:\Python26\lib\site-packages\dojango\forms__init__.py”, 第 2 行,在 from widgets import * 文件 "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 行,在 对于设置中的应用程序。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 中 基 = find_app_dir(app_name) 文件 “C:\ Python26 \ lib \ site-packages \ dojango \ util \ media.py”, 第 20 行,在 find_pp_dir 中 mod = getattr(导入(m, {}, {}, [a]), a)

评论应用位于已安装的应用中。 我应该怎么办?

编辑: 如果我尝试直接使用 import forms 包含表单,我会得到以下结果:

回溯(最近一次调用最后一次): 文件“I:\wamp\www\Website\manage.py”,第 11 行,位于 执行管理器(设置)
文件“C:\Python26\lib\site-packages\django\core\management__init__.py”,行 438,在execute_manager中 实用程序.execute()
文件“C:\Python26\lib\site-packages\django\core\management__init__.py”,行 379,执行中 self.fetch_command(子命令).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”,林 e 66,激活中 返回 real_activate(语言)
文件“C:\Python26\lib\site-packages\django\utils\features.py”,第 55 行,在 _ 咖喱 返回 _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
文件“C:\Python26\lib\site-packages\django\utils\translation__init__.py”,林 e 36,在delayed_loader中 返回 getattr(trans, real_name)(*args, **kwargs)
文件“C:\Python26\lib\site-packages\django\utils\translation\trans_real.py”,l ine 193,激活中 _active[currentThread()] = 翻译(语言)
文件“C:\Python26\lib\site-packages\django\utils\translation\trans_real.py”,l 176,翻译中 default_translation = _fetch(settings.LANGUAGE_CODE)
文件“C:\Python26\lib\site-packages\django\utils\translation\trans_real.py”,l ine 159,在 _fetch 中 应用程序 = import_module(应用程序名称)
文件“C:\Python26\lib\site-packages\django\utils\importlib.py”,第 35 行,在 im 端口模块 导入(名称)
文件“I:\wamp\www\Website\Comments__init__.py”,第 2 行,位于 导入表格
文件“I:\wamp\www\Website\Comments\forms.py”,第 3 行,位于 从 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 行,位于 对于设置中的应用程序。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_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 基= find_app_dir(应用程序名称)
文件“C:\Python26\lib\site-packages\dojango\util\media.py”,第 20 行,位于 find_a 中 目录 mod = getattr(导入(m, {}, {}, [a]), a)
AttributeError:“模块”对象没有属性“注释”

删除对 dojango 的任何引用可以解决该问题。

I get:

TemplateSyntaxError at /blog/post/test
Caught NameError while rendering:
global name 'forms' is not defined

for this code:
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

importing form directly inside init.py results in:

AttributeError: 'module' object has no
attribute 'Comments'

Here's the stack trace, the error appears to be coming from dojango but that doesn't really make sense:

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)

The Comments app is in the installed apps.
What should I do?

EDIT:
If I try to include forms directly with import forms I get this:

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'

Removing any reference for dojango solves the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笔一画续写前缘 2024-09-20 04:43:09

这是 dojango 中的一个错误。
我会报告的。

This is a bug in dojango.
I will report it.

时光是把杀猪刀 2024-09-20 04:43:09

将以下内容放入 __init__.py 中:

import forms

put the following in __init__.py:

import forms
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文