Opened 4 years ago

Last modified 3 years ago

#27325 assigned New feature

Offer a solution for static file serving suitable for production use

Reported by: Tim Graham Owned by: Aleksej Manaev
Component: Core (Other) Version: master
Severity: Normal Keywords:
Cc: Aleksej Manaev Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Mads Jensen)

The documentation for the function for serving static files, django.contrib.staticfiles.views.serve(), describes it as "grossly inefficient and probably insecure. This is only intended for local development, and should never be used in production." To ease the deployment of small sites, it would be nice to fix or replace it with something that could be used in production. WhiteNoise is an example of static file serving done in Python and is a possible candidate for integration in Django.

Ideas are discussed in more detail on the delegating our static file serving thread on django-developers.

Change History (7)

comment:1 Changed 4 years ago by Aleksej Manaev

Cc: Aleksej Manaev added
Owner: changed from nobody to Aleksej Manaev
Status: newassigned

comment:2 Changed 4 years ago by Mads Jensen

Description: modified (diff)

comment:3 Changed 3 years ago by Aleksej Manaev

Has patch: set

comment:4 Changed 3 years ago by Tim Graham

Patch needs improvement: set

I agree with what Florian said on the mailing list, "I think there is no need to vendor [whitenoise], we can just depend on it. (like we do for pytz)". The Django-specific parts can be moved in Django. Also, Python 2 compatibility can be removed since the master branch doesn't support it anymore. Tests and documentation are also required.

comment:5 Changed 3 years ago by Aleksej Manaev

If we deprecate django.contrib.staticfiles.handlers.StaticFilesHandler and replace it by DjangoWhiteNoise in django.contrib.staticfiles.testing.StaticLiveServerTestCase it would not be possible to run tests without using collectstatic first. Is this an important behavior?

tests/staticfiles_tests/test_liverserver.StaticLiveServerView.test_collectstatic_emulation only passes with DEBUG=True.

"django.core.exceptions.ImproperlyConfigured: WHITENOISE_USE_FINDERS can only be enabled in development when WHITENOISE_AUTOREFRESH is also enabled."

Now I do not get errors using WHITENOISE_USE_FINDERS=True and WHITENOISE_AUTOREFRESH=True which can be used in StaticLiveServerTestCase to achieve running tests without collectstatic first.


  • tests/staticfiles_tests/test_liverserver.StaticLiveServerViewUseFinders.test_collectstatic_emulation
  • tests/staticfiles_tests/test_liverserver.TestWhitenoiseMiddlewareUseFinders.test_collectstatic_emulation

Does anybody know why overriding_settings within a test method has no effect?

For example:

    response = self.client.get('/static/test/file.txt')
    content = b''
    for item in response.streaming_content:
      content += item
    self.assertEqual(content.rstrip(b'\r\n'), b'In static directory.')
Last edited 3 years ago by Aleksej Manaev (previous) (diff)

comment:6 Changed 3 years ago by Aleksej Manaev

Patch needs improvement: unset

comment:7 Changed 3 years ago by Tim Graham

Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top