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



我正在尝试按照这里的步骤操作:

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 运行服务器"时,我收到以下错误:

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__'

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

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

相关内容

  • 没有找到相关文章

最新更新