Django:尝试添加新应用时出错

时间:2012-05-10 08:54:31

标签: django django-apps django-comments

我正在尝试按照此处的步骤进行操作:

http://dev.svetlyak.ru/optional-email-in-django-comments-en/

使Django评论应用中的“电子邮件地址”字段可选。具体来说,我创建了一个名为“mycomments.py”的文件,其中包含以下内容:

from django import forms
from django.contrib.comments.forms import CommentDetailsForm
from django.utils.translation import ungettext, ugettext_lazy as _

class CommentForm(CommentDetailsForm):
    email = forms.EmailField(label=_("Email address"), required=False)

def get_form():
    return CommentForm

并将其放在我的Django项目的根文件夹中(包含manage.py和settings.py的文件夹)。然后,我将“mycomments”添加到settings.py文件中,如下所示:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django.contrib.comments',
    'blogs',
    'mycomments',
)

COMMENTS_APP = 'mycomments'

但是当我做'python manage.py runserver'时,我收到以下错误:

Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x8bb208c>>
Traceback (most recent call last):

File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 88, in inner_run
self.validate(display_num_errors=True)

File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 249, in validate
num_errors = get_validation_errors(s, app)

File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 35, in get_validation_errors
for (app_name, error) in get_app_errors().items():

File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 146, in get_app_errors
self._populate()

File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 61, in _populate
self.load_app(app_name, True)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 83, in load_app
if not module_has_submodule(app_module, 'models'):

File "/usr/local/lib/python2.7/dist-packages/django/utils/module_loading.py", line 17, in module_has_submodule
for entry in package.__path__:  # No __path__, then not a package.

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

开发服务器无法启动。我做错了吗?

1 个答案:

答案 0 :(得分:1)

Django应用程序应该(至少)包含__init__.pymodels.py个文件的目录。因此,请创建mycomments目录,将代码放入__init__.py并在其中添加空models.py