On 29 Sep 2014, at 22:46, Jeremy Dunck <
jdu...@gmail.com> wrote:
> Right now, I think that static/media handling is fairly confused in the documentation, and a bit confused in the code itself.
Please make sure to open tickets about this, I’ve long lost the ability to see at this code from the beginners perspective. Any specific problems with the docs would be appreciated. I understand that we can only do so much of course by fixing the docs.
> We have a few special-cases floating around:
>
> default_storage (needed for legacy before storage backends)
Yeah, the naming is unfortunate, but really only a legacy thing when storage only meant “upload API”. We could rename it into “upload_storage”?
> staticfiles_storage (needed for collectstatic/handling)
> {% static %} handles mapping relative URLs to absolute URLs, but does not allow using a storage engine other than {% STATIC_URL %} or staticfiles_storage.
>
> I was surprised to find that
> django.contrib.staticfiles.templatetags
> and
> django.templatetags.static
> both have implementations of {% static %} with slightly different semantics (one based on STATIC_URL, one based on staticfiles_storage).
The historic reason for this is simply the requirement for being able to disable the staticfiles contrib app. It was decided when I worked on this to be a blocker for a merge.
So I implemented the static tag twice and made the core tag as simple as possible. Using urljoin with STATIC_URL was the recommended thing to do at the time after all. staticfiles couldn’t go that route directly though as it needed some better API and especially something that can be extended. The storage system was there so I used it.
In hindsight I regret not having pushed harder for integration of staticfiles into core instead, lots of confusion could have been prevented, IMO.
> It seems to me that it might be useful to introduce aliases (as in CACHES and DATABASES) in order to allow referring to storage engines in a less-coupled way.
>
> {% static %} could then take a storage engine alias, and the special-case of repo-static file handling and user-uploaded file handling could mostly go away.
>
> MEDIA_URL/MEDIA_ROOT and STATIC_URL/STATIC_ROOT as special cases could (after a deprecation process) go away in favor of these storage aliases.
>
> The syntax I'll hazard as a proposal could be:
>
> FILE_STORES = {
> 'static': <Storage instance>,
> 'dynamic': <Storage instance>,
> ....
> 'alias1': 'alias2' # possibly map aliases to satisfy app-required aliases.
> }
>
> {% file relative-url storage-alias %}
>
> Would do people think of this idea?
That implies that we stop referring to uploaded files directly via the FieldField instance attribtue, e.g. {{ my_model.uploaded_file.url }}. I’m okay with that, if your proposed tag would be able to take file field objects as well as relative paths.
On a related note, could we simply rename the MEDIA_* settings to UPLOAD_* and just fix this ambiguity in other places as well?
Jannis