1

我有点难过。在开发中,我试图在 DJango 1.3 中为我的应用程序提供静态和动态文件。我喜欢新的静态功能,但我似乎无法让它正常工作。

当我阅读文档时,看起来以下应该有效。它提供动态的东西很好,但不是静态的。

urlpatterns += staticfiles_urlpatterns()

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/dynamic/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
   )
4

2 回答 2

7

在 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,
        }),
    )
于 2011-06-23T06:05:59.957 回答
1

从 settings.py检查STATIC_URL,值是多少?

于 2011-06-24T09:36:12.823 回答