Django静态文件网址设置

时间:2014-11-24 04:21:38

标签: python django

请帮我查看我的static设置

Debug =False时,效果很好 但是当Debug=True时,它无法捕获静态文件

请帮帮我,谢谢。

这是我的settings.py:

DEBUG = False
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] 

urls.py

from django.conf import settings
urlpatterns = patterns('',
    url(r'', include('core.urls', namespace='core')),
)
if settings.DEBUG is False:    
    urlpatterns += patterns('',
            url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    )
else:
    from django.conf.urls.static import static
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

1 个答案:

答案 0 :(得分:0)

如果您将在urls.py中编写以下代码,它将同时适用于DEBUG状态

url( r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT } ),

有一点需要注意我的静态根就像django 1.4 +

PROJECT_DIR = os.path.dirname(os.path.dirname( __file__ ))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

与此同时,您需要执行以下命令将所有静态文件复制到PROJECT_PATH / static目录

python manage.py collectstatic