11

Using the django dev server (1.7.4), I want to add some headers to all the static files it serves.

It looks like I can pass a custom view to django.conf.urls.static.static, like so:

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)

And common.views.static.serve looks like this:

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

However, simply having django.contrib.staticfiles in INSTALLED_APPS adds the static urls automatically, and there doesn't seem to be a way to override them. Removing django.contrib.staticfiles from INSTALLED_APPS makes this work, however, if I do that, the staticfiles templatetags are no longer available.

How can I override the headers that are served for static files using the django development server?

synic
  • 26,359
  • 20
  • 111
  • 149
  • hmmm, I assume you have your reasons, but could you not achieve this by defining the headers as an include within a block on a generic template and then extend that template on your actual templates. – JL Peyret Feb 25 '15 at 17:45
  • It's not for html files, it's for font files. – synic Feb 25 '15 at 17:46
  • 2
    oh, ok. well, like I said, I assumed you had your reasons which is why I didn't write it up as an answer. – JL Peyret Feb 25 '15 at 17:53

2 Answers2

6

staticfiles app overrides the core runserver command but allows you to disable the automatic serving of the static files:

python manage.py runserver --nostatic
catavaran
  • 44,703
  • 8
  • 98
  • 85
0

I found the code of the author did not work for me, I would get errors like:

[10/Dec/2020 18:08:13] "GET /static/img/foo.svg HTTP/1.1" 404 10482
Not Found: /static/img/foo.svg

I am using Django 3 if that makes a difference.

This is what I did:

from django.contrib.staticfiles.views import serve

def custom_serve(request, path, insecure=False, **kwargs):
    """
    Customize the response of serving static files.

    Note:
        This should only ever be used in development, and never in production.
    """
    response = serve(request, path, insecure=True)
    response['Access-Control-Allow-Origin'] = '*'
    # if path.endswith('sw.js'):
    #    response['Service-Worker-Allowed'] = '/'
    return response

the urls part is the same as the question:

from django.conf import settings

if settings.DEBUG:
    # Allow custom static file serving (use with manage.py --nostatic)
    from django.conf.urls.static import static
    from CHANGE.THIS.PATH.views import custom_serve
    urlpatterns += static(
        settings.STATIC_URL, document_root=settings.STATIC_ROOT, view=custom_serve
    )
run_the_race
  • 1,344
  • 2
  • 36
  • 62