django dev服务器,将标头添加到静态文件

syn*_*nic 9 python django django-staticfiles

使用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)
Run Code Online (Sandbox Code Playgroud)

而且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
Run Code Online (Sandbox Code Playgroud)

然而,仅仅拥有django.contrib.staticfilesINSTALLED_APPS自动添加静态URL,并且似乎没有被覆盖它们的方式.但是,如果我这样做,则删除django.contrib.staticfiles可以INSTALLED_APPS使静态文件模板标签不再可用.

如何使用django开发服务器覆盖为静态文件提供的标头?

cat*_*ran 5

staticfilesapp 覆盖核心runserver命令,但允许您禁用静态文件的自动提供:

python manage.py runserver --nostatic
Run Code Online (Sandbox Code Playgroud)