Import fails with a strange error
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:wampwwwWebsiteComments__init__.py", line 1, in from Website.Comments import models, forms File "I:wampwwwWebsiteCommentsforms.py", line 1, in from dojango import forms File "C:Python26libsite-packagesdojangoforms__init__.py", line 2, in from widgets import * File "C:Python26libsite-packagesdojangoformswidgets.py", line 11, in from dojango.util.config import Config File "C:Python26libsite-packagesdojangoutilconfig.py", line 3, in from dojango.util import media File "C:Python26libsite-packagesdojangoutilmedia.py", line 49, in for app in settings.INSTALLED_APPS) File "C:Python26libsite-packagesdojangoutilmedia.py", line 49, in for app in settings.INSTALLED_APPS) File "C:Python26libsite-packagesdojangoutilmedia.py", line 38, in find_ pp_dojo_dir_and_url media_dir = find_app_dojo_dir(app_name) File "C:Python26libsite-packagesdojangoutilmedia.py", line 27, in find_ pp_dojo_dir base = find_app_dir(app_name) File "C:Python26libsite-p ackagesdojangoutilmedia.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:wampwwwWebsitemanage.py", line 11, in execute_manager(settings)
File "C:Python26libsite-packagesdjangocoremanagement__init__.py", line 438, in execute_manager utility.execute()
File "C:Python26libsite-packagesdjangocoremanagement__init__.py", line 379, in execute self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:Python26libsite-packagesdjangocoremanagementbase.py", line 191, in run_from_argv self.execute(*args, **options. dict )
File "C:Python26libsite-packagesdjangocoremanagementbase.py", line 209, in execute translation.activate('en-us')
File "C:Python26libsite-packagesdjangoutilstranslation__init__.py", lin e 66, in activate return real_activate(language)
File "C:Python26libsite-packagesdjangoutilsfunctional.py", line 55, in _ curried return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
File "C:Python26libsite-packagesdjangoutilstranslation__init__.py", lin e 36, in delayed_loader return getattr(trans, real_name)(*args, **kwargs)
File "C:Python26libsite-packagesdjangoutilstranslationtrans_real.py", l ine 193, in activate _active[currentThread()] = translation(language)
File "C:Python26libsite-packagesdjangoutilstranslationtrans_real.py", l ine 176, in translation default_translation = _fetch(settings.LANGUAGE_CODE)
File "C:Python26libsite-packagesdjangoutilstranslationtrans_real.py", l ine 159, in _fetch app = import_module(appname)
File "C:Python26libsite-packagesdjangoutilsimportlib.py", line 35, in im port_module import (name)
File "I:wampwwwWebsiteComments__init__.py", line 2, in import forms
File "I:wampwwwWebsiteCommentsforms.py", line 3, in from dojango.forms import fields, widgets
File "C:Python26libsite-packagesdojangoforms__init__.py", line 2, in from widgets import *
File "C:Python26libsite-packagesdojangoformswidgets.py", line 11, in from dojango.util.config import Config
File "C:Python26libsite-packagesdojangoutilconfig.py", line 3, in from dojango.util import media
File "C:Python26libsite-packagesdojangoutilmedia.py", line 49, in for app in settings.INSTALLED_APPS)
File "C:Python26libsite-packagesdojangoutilmedia.py", line 49, in for app in settings.INSTALLED_APPS)
File "C:Python26libsite-packagesdojangoutilmedia.py", line 38, in find_a pp_dojo_dir_and_url media_dir = find_app_dojo_dir(app_name)
File "C:Python26libsite-packagesdojangoutilmedia.py", line 27, in find_a pp_dojo_dir base = find_app_dir(app_name)
File "C:Python26libsite-packagesdojangoutilmedia.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.
This is a bug in dojango.
I will report it.
在__init__.py中放入以下内容:
import forms
链接地址: http://www.djcxy.com/p/40566.html
上一篇: 无法使用Ubuntu上的Django连接到Oracle数据库
下一篇: 导入失败并出现奇怪的错误