您的目录结构也可能取决于您使用的 django 版本。如果您使用的是 django 1.3,则处理静态内容略有变化。您的模板也可以单独排列。
以下仅适用于 django 1.3。
在app 目录中:
...
app1/
static/
app1/
templates/
app1/
models.py
...
views.py
如果您使用新的django.contrib.staticfiles 应用程序,您的设置可能如下所示:
MEDIA_ROOT = path.join(ROOT_PATH,'uploaded_media/')
MEDIA_URL = '/uploaded_media/'
# static content is collected here, and served from here, but don't add stuff manually here! add to staticfiles_dirs
STATIC_ROOT = path.join(ROOT_PATH, 'collected_static/')
ADMIN_MEDIA_PREFIX = '/static/admin/'
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
path.join(ROOT_PATH, 'src/extra_static/'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
同样,您的模板可以直接从INSTALLED_APP 加载:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader'
)
TEMPLATE_DIRS = (
path.join(ROOT_PATH,'src/templates/'),
)
上述两种策略意味着模板和静态内容可以存在于其特定的应用目录中。在开发中,使用contrib.staticfiles,可以直接从您的应用程序文件夹中提供静态内容。在生产中,有一个管理命令将所有应用目录静态内容收集到/path/to/project/collected_static/,您可以将您的网络服务器指向该目录以提供静态内容。
对于预打包的库,使用 virtualenv 和 pip 是个好主意。否则,我喜欢将库保存在项目根目录中的 lib 目录中。它使得引用源代码、模板和静态内容非常方便,而不是安装到 site-packages(尤其是在不使用 virtualenv 时)。
所以,重新安排你的项目结构:
webapps/
myproject/
apache/
bin/
lib/
collected_static/
uploaded_media/
myproject.wsgi
src/
templates/ # lib template overrides and site wide templates
base.html
lib_1/
nav.html
extra_static/
lib_1/ # libs that dont support django 1.3 static
js/
css/
settings.py
settingslocal.py # keep developer specific settings here
urls.py
manage.py
app1/
__init.py
static/
app1/
js/
css/
templates/
app1/