Django项目配置错误

2 投票
1 回答
1273 浏览
提问于 2025-04-18 14:06

我刚开始学习Django,了解了基本的Django MVC架构。经过一些搜索,我发现我们应该按照下面的目录结构来组织项目,而不是使用django-admin.py startproject myprojmanage.py startapp app1自动生成的结构。

现在我的Django项目结构是这样的。(为了简化问题,我省略了一些内容,比如日志、fabfile等)

ROOT_DIR
    |-- manage.py
    |-- readme.txt
    |-- requirements.txt
    `-- webapp
        |-- apps
        |   |-- app1
        |   |   |-- admin.py
        |   |   |-- __init__.py
        |   |   |-- models.py
        |   |   |-- tests.py
        |   |   |-- urls.py
        |   |   `-- views.py
        |   |-- app2
        |   |   |-- admin.py
        |   |   |-- __init__.py
        |   |   |-- models.py
        |   |   |-- tests.py
        |   |   |-- urls.py
        |   |   `-- views.py
        |   `-- __init__.py
        |-- __init__.py
        |-- settings
        |   |-- __init__.py
        |   |-- common.py
        |   |-- development.py
        |   `-- production.py
        |-- static #CSS,JS,Images...
        |-- templates
        |-- urls.py
        `-- wsgi.py

wsgi.py

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "webapp.settings.development")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

manage.py

import os,sys
if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "webapp.settings.development")
    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)

但是当我运行python manage.py runserver时,出现了这个错误。

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/eric/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/home/eric/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/eric/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/eric/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 280, in execute
    translation.activate('en-us')
  File "/home/eric/venv/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 130, in activate
    return _trans.activate(language)
  File "/home/eric/venv/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 188, in activate
    _active.value = translation(language)
  File "/home/eric/venv/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 177, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/home/eric/venv/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 159, in _fetch
    app = import_module(appname)
  File "/home/eric/venv/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
ImportError: No module named app2

我在development.py的开头和结尾放了一个检查打印语句。print INSTALLED_APPS

当我运行服务器时,它打印出了这个INSTALLED_APPS列表的所有值(也就是说,我在development.py中获取到的所有应用。INSTALLED_APPS变量在common.py中,而development.py是从common.py导入的)。

我不知道自己哪里出错了。(这是不是因为我把installed_apps变量用列表而不是元组?而且我使用了from common import *,但我觉得这个导入语句不是导致这个错误的主要原因。我是否需要导入其他东西,比如from __future__ import ...*

附言:如果你对结构有任何建议,我非常欢迎,但我更关心这个错误。

编辑:

common.py

import os
import sys

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_DIR = os.path.join(BASE_DIR, "webapp")

sys.path.append(PROJECT_DIR + '/apps')

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'south',
    'app1',
    'app2',
]

...

1 个回答

0

看起来 webapp/apps 这个路径没有在你的环境变量里,所以系统找不到 app2 模块。

我发现我总是需要修改 PYTHONPATH,原因有很多……每个项目或环境最后都会有一个 init_pythonpath.py 文件:

import sys

def init_pythonpath():
    path = '/home/patrick/path/to/project'
    venv = '/home/patrick/.virtualenvs/venv_name/lib/python2.7/site-packages'
    if venv not in sys.path:
        sys.path.insert(0, venv)
    if path not in sys.path:
        sys.path.insert(0, path)

然后,在 manage.pywsgi.py 文件里,我会加上这个设置:

import imp
init_pythonpath_path = os.path.join(os.path.dirname(__file__), 'init_pythonpath.py')
init_pythonpath = imp.load_source('init_pythonpath', init_pythonpath_path)
init_pythonpath.init_pythonpath()

import sys

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

# ...

这可能对你有用。

撰写回答