导入失败并出现奇怪错误

我明白了:

/ 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

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中导入表单会导致:

AttributeError:'module'对象没有属性'Comments'

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

从Website.Comments导入模型文件“I:\ wamp \ www \ Website \ Comments__init __ . py”,第1行,表单文件“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行,in settings in settings.INSTALLED_APPS)文件“C:\ Python26 \ lib \ site-packages \ dojango \ util \ media .py“,第49行,用于settings.INSTALLED_APPS中的app)文件”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 = getattr(import(m,{},{},[a]),a)

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

编辑:如果我尝试直接包含表单 import forms 我得到这个:

Traceback(最近一次调用最后一次):文件“I:\ wamp \ www \ Website \ manage.py”,第11行,在execute_manager(设置)文件“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(子命令).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“,lin e 66,在激活中返回real_activate(语言)文件”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”,lin e 36,在delayed_loader中返回getattr(trans,real_name)(* args,** kwargs)文件“C:\ Python26 \ lib \ site-packages \ django \ utils \ translation \ trans_real.py”,l ine 193,在activate _active [currentThread] ()] =翻译(语言)文件“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,在_fetch app = import_module(appname)文件”C:\ Python26 \ lib \ site-packages \ django \ utils \ importlib .py“,第35行,在im port_module导入(名称)文件”I:\ wamp \ www \ Website \ Comments__init __ . py“,第2行,导入表单文件”I:\ wamp \ www \ Website \ Comments \ forms .j“,第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 import Config File”C:\ Python 26 \ lib \ site-packages \ dojango \ util \ config.py“,第3行,来自dojango.util导入媒体文件”C:\ Python26 \ lib \ site-packages \ dojango \ util \ media.py“,line 49,for for app in settings.INSTALLED_APPS)文件“C:\ Python26 \ lib \ site-packages \ dojango \ util \ media.py”,第49行,在for 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“,line 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:'module'对象没有属性'Comments'

删除dojango的任何引用解决了这个问题 .

回答(2)

2 years ago

这是dojango中的一个错误 .
我会举报 .

2 years ago

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

import forms