I get:
TemplateSyntaxError at /blog/post/test Caught NameError while rendering: global name 'forms' is not defined
for this code:
forms.pyfrom 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
开发者_StackOverflow中文版
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.
This is a bug in dojango.
I will report it.
put the following in __init__.py:
import forms
精彩评论