我想使用django dev服务器(1.7.4),向其服务的所有静态文件中添加一些标头。
看来我可以将自定义视图传递给django.conf.urls.static.static
,如下所示:
if settings.DEBUG:
from django.conf.urls.static import static
from common.views.static import serve
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT, view=serve)
而且common.views.static.serve
是这样的:
from django.views.static import serve as static_serve
def serve(request, path, document_root=None, show_indexes=False):
"""
An override to `django.views.static.serve` that will allow us to add our
own headers for development.
Like `django.views.static.serve`, this should only ever be used in
development, and never in production.
"""
response = static_serve(request, path, document_root=document_root,
show_indexes=show_indexes)
response['Access-Control-Allow-Origin'] = '*'
return response
但是,简单地使用django.contrib.staticfiles
inINSTALLED_APPS
会自动添加静态url,并且似乎没有一种方法可以覆盖它们。django.contrib.staticfiles
从中删除INSTALLED_APPS
会使这项工作有效,但是,如果我这样做了,则静态文件templatetags将不再可用。
如何使用django开发服务器覆盖为静态文件提供的标头?
staticfiles
应用程序覆盖了核心runserver
命令,但允许您禁用自动提供静态文件:
python manage.py runserver --nostatic
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句