在 django 1.3 中,静态和动态内容已经分离。要使用新功能,请像这样设置您的项目:
project
|- app1
|- media # exists only on server/folder for dynamic content
|- static-root # exists only on server/folder for static content
|- static # folder for site-specific static content
|- settings.py
|- manage.py
`- urls.py
设置.py
from os import path
PROJECT_ROOT = path.dirname(path.abspath(__file__)) #gets directory settings is in
#-- dynamic content is saved to here --
MEDIA_ROOT = path.join(PROJECT_ROOT,'media')
MEDIA_URL = '/media/'
#-- static content is saved to here --
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root') # this folder is used to collect static files in production. not used in development
STATIC_URL = "/static/"
ADMIN_MEDIA_URL = STATIC_URL + 'admin/' #admin is now served by staticfiles
STATICFILES_DIRS = (
('site', path.join(PROJECT_ROOT,'static')), #store site-specific media here.
)
#-- other settings --
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
网址.py
from django.conf import settings
#your URL patterns
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns() #this servers static files and media files.
#in case media is not served correctly
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)