Django项目配置

2024-04-24 19:42:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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

^{pr2}$

管理.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中放置了check print语句。 print INSTALLED_APPS

当我运行服务器时,它会打印出这个INSTALLED_APPS列表的所有值(我是说我要进入的所有应用程序开发.py. INSTALLED\u APPS变量位于普通.py以及开发.py正在导入普通.py). 在

我不知道我在哪里犯错。(这是因为我使用的是list而不是tuple for installed\u apps变量?我使用的是from common import *,但我不认为这个import语句是导致这个错误的主要原因。我是否必须导入其他东西,如from __future__ import ...*

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

编辑:

普通.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',
]

...

Tags: djangoinfrompyimporthomevenvinit
1条回答
网友
1楼 · 发布于 2024-04-24 19:42:13

看起来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中添加以下咒语:

^{pr2}$

可能对你有用。在

相关问题 更多 >