【发布时间】:2014-09-12 01:43:55
【问题描述】:
我是 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 中添加了一个检查打印语句。
print INSTALLED_APPS
当我运行服务器时,它会打印此INSTALLED_APPS 列表的所有值(我的意思是我在 development.py 中获得的所有应用程序。INSTALLED_APPS 变量在 common.py 中,而 development.py 正在导入 common.py)。
我不知道我在哪里犯了错误。 (这是因为我使用列表而不是元组作为 installed_apps 变量吗?我使用的是from common import *,但我不认为这个导入语句是这个错误的主要原因。我是否必须导入一些其他的东西,比如@987654332 @)
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',
]
...
【问题讨论】:
-
我还推荐
python manage.py startapp app_name来创建您的应用程序 -
是的,没关系。正如您在我的问题详细信息中看到的那样,我使用它
-
你能显示INSTALLED_APPS的值吗?
-
是的,它是一个简单的列表,其中包含所有应用程序,包括 django 和第三方应用程序,例如 south。@DanielRoseman 这不是一个元组,这就是原因?
-
@user3810188 我打赌你的目录结构(这似乎不是由
manage.py设置的)正在抛出manage.py。
标签: python django python-2.7 python-3.x django-admin