Code

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#15199 closed (wontfix)

Allow MEDIA_ROOT inside STATIC_ROOT

Reported by: brutasse Owned by:
Component: contrib.staticfiles Version: master
Severity: Keywords: blocker
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:

Description

I have the following layout:

STATIC_ROOT = '/path/to/static/'
STATIC_URL = '/static/'

MEDIA_ROOT = STATIC_ROOT + 'media/'
MEDIA_URL = STATIC_URL + 'media/'

Basically, MEDIA_ROOT is a subdirectory of STATIC_ROOT.

With runserver, I won't be able to serve my media files. When I try to fetch a media file, the StaticFilesHandler tries to handle it (since its URL starts with STATIC_URL) but no finder will be able to resolve its path.

The handler then raises a 404 even if I have a pattern in my urlconf to serve my media files.

After discussing it on IRC, it looks like two things are needed to support this:

  • Patch the contrib.staticfiles handler to specifically ignore anything inside MEDIA_URL
  • Add extra checks to the collectstatic management command to make sure collectstatic won't write anything inside MEDIA_ROOT (which could potentially overwrite some user data).

Attachments (2)

15199-staticfiles-handler.patch (805 bytes) - added by brutasse 3 years ago.
Patch for the StaticFilesHandler
15199-collectstatic.patch (973 bytes) - added by brutasse 3 years ago.
Patch for collectstatic, probably needs some improvements

Download all attachments as: .zip

Change History (8)

Changed 3 years ago by brutasse

Patch for the StaticFilesHandler

Changed 3 years ago by brutasse

Patch for collectstatic, probably needs some improvements

comment:1 Changed 3 years ago by brutasse

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement set

comment:2 follow-up: Changed 3 years ago by brutasse

Hmmm this is trickier. With the patch applied, the media files aren't served by staticfiles but defining a custom view won't work either.

urlpatterns += patterns('django.contrib.staticfiles.views',
    url(r'^static/media/(?P<path>.*)$', 'serve',
        {'document_root': settings.MEDIA_ROOT}),
)

The 'serve' view is called, but the options dict is not passed to the view. 'document_root' is swallowed somewhere and ends up as None in the view, something bad probably happens in the staticfiles handler. If I change MEDIA_URL to '/static_media/' and the URL pattern to r'^static_media/(?P<path>.*)$', the options dict is correctly passed to the view.

I thought modifying _should_handle() on StaticFilesHandler would do the job, but apparently it's not enough. The staticfiles handler isn't transparently giving the control back to the parent WSGIHandler if _should_handle returns false.

comment:3 Changed 3 years ago by carljm

  • Keywords blocker added
  • Triage Stage changed from Unreviewed to Accepted

This is a 1.3 blocker, because it's a significant problem with a new feature in 1.3.

comment:4 in reply to: ↑ 2 Changed 3 years ago by jezdez

Replying to brutasse:

The 'serve' view is called, but the options dict is not passed to the view. 'document_root' is swallowed somewhere and ends up as None in the view, something bad probably happens in the staticfiles handler. If I change MEDIA_URL to '/static_media/' and the URL pattern to r'^static_media/(?P<path>.*)$', the options dict is correctly passed to the view.

Not sure what you mean with "something bad happens", the view looks just fine: source:django/trunk/django/contrib/staticfiles/views.py#L23

Last edited 3 years ago by jezdez (previous) (diff)

comment:5 Changed 3 years ago by carljm

  • Resolution set to wontfix
  • Status changed from new to closed

After further IRC discussion with jezdez, closing this wontfix. Supporting a configuration with MEDIA_ROOT inside STATIC_ROOT introduces a number of additional complexities and couplings between staticfiles and the MEDIA_* settings, which we are trying to avoid, and it's not clear what meaningful benefits it buys us. The main mentioned benefit was to only require one alias on the front-end webserver: that seems minor, since an alias is e.g. just one line in an nginx conf file. In any case, the same result can be achieved by putting MEDIA_ROOT and STATIC_ROOT side by side in a parent directory, and aliasing the front-end webserver to that parent directory.

comment:6 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.