Opened 8 years ago

Last modified 6 weeks ago

#27325 new New feature

Offer a solution for static file serving suitable for production use

Reported by: Tim Graham Owned by:
Component: Core (Other) Version: dev
Severity: Normal Keywords:
Cc: Aleksej Manaev, Ülgen Sarıkavak Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
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 (9)

comment:1 by Aleksej Manaev, 8 years ago

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

comment:2 by Mads Jensen, 7 years ago

Description: modified (diff)

comment:3 by Aleksej Manaev, 7 years ago

Has patch: set

comment:4 by Tim Graham, 7 years ago

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 by Aleksej Manaev, 7 years ago

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.

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

EDIT:
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.
See: tests/staticfiles_tests/test_liverserver.StaticLiveServerViewUseFinders.test_collectstatic_emulation
See: tests/staticfiles_tests/test_liverserver.TestWhitenoiseMiddlewareUseFinders.test_collectstatic_emulation

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

For example:

  with self.settings(WHITENOISE_USE_FINDERS=True, WHITENOISE_AUTOREFRESH=True):
    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.')
Version 6, edited 7 years ago by Aleksej Manaev (previous) (next) (diff)

comment:6 by Aleksej Manaev, 7 years ago

Patch needs improvement: unset

comment:7 by Tim Graham, 7 years ago

Patch needs improvement: set

comment:8 by Carlton Gibson, 23 months ago

Has patch: unset
Owner: Aleksej Manaev removed
Patch needs improvement: unset
Status: assignednew

comment:9 by Ülgen Sarıkavak, 6 weeks ago

Cc: Ülgen Sarıkavak added
Note: See TracTickets for help on using tickets.
Back to Top