我是Django的新手。我理解基本的Django MVC架构。
经过一点点的谷歌搜索后,我发现我们应该遵循以下目录结构,而不是使用django-admin.py startproject myproj
和manage.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
的文件的开头和结尾放了一个check print语句。
print INSTALLED_APPS
当我运行runserver时会打印出这个INSTALLED_APPS
列表的所有值(我的意思是我在develop.py中获得的所有应用程序.INSTALLED_APPS变量在common.py中,而development.py正在导入common.py)
我不知道我哪里弄错了。 (这是因为我使用list而不是tuple来安装instal_apps变量吗?我正在使用from common import *
但我不认为这个import语句是导致此错误的主要原因。我是否必须导入其他类似的东西from __future__ import ...*
)
PS:如果你有任何与结构有关的建议,那么最受欢迎,但我更关心这个错误。
修改
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',
]
...
答案 0 :(得分: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.py
或wsgi.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")
# ...
它可能适合你。