我是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
^{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',
]
...
看起来
webapp/apps
不在您的路径上,因此它不会在那里查找并找到app2
模块。在我发现由于各种原因,我总是不得不修改
PYTHONPATH
。。。每个项目/环境都以一个init_pythonpath.py
文件结束:然后,在
^{pr2}$manage.py
或wsgi.py
中添加以下咒语:可能对你有用。在
相关问题 更多 >
编程相关推荐