Docs improvement? - MEDIA_URL in templates

255 views
Skip to first unread message

mjnich...@gmail.com

unread,
Jan 30, 2021, 10:56:36 AM1/30/21
to Django developers (Contributions to Django itself)
Not sure if this is something worth posting here or not, but I was encouraged to put it up for discussion at least by somebody that knows more than me :) 

I was recently wanting to display user-uploaded media from a template (an avatar pic in my navbar), and I couldn't really work out the "right" way of doing this  from the docs. Docs for using static files in templates are comprehensive - less so for media files from what I can tell. 

A few SO questions, a bit of trial and error, and finding the https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#get-media-prefix docs led me to the solution where I have

<body data-media-url="{% get_media_prefix as MEDIA_PREFIX %}">

in my base template, then 

<img src="{{ MEDIA_PREFIX }}{{ user.avatar }}" 

in my navbar. 

This works nicely, though I still don't really know if it would be regarded as the "right" way to do it or if I now have a somewhat "hacky" solution :)

On the page

https://docs.djangoproject.com/en/3.1/howto/static-files/

it talks about deploying static and media files in prod, and gives an example of  how to use static files in a template, but no example for media files. It strikes me that it would be handy if this page gave an example of how to include a media file in a template. 

Hope I'm not wasting people's time here!

Cheers

Mike

Tim Graham

unread,
Jan 30, 2021, 11:16:17 AM1/30/21
to Django developers (Contributions to Django itself)
Hi Mike,

I think you want to use {{ user.avatar.url }}.


For future reference, since this is more of a usage question, it would be better to post to django-users. If you want to suggest a documentation change after some discussion there, you can create a ticket at https://code.djangoproject.com/.

Mike Nicholson

unread,
Jan 30, 2021, 5:47:00 PM1/30/21
to django-d...@googlegroups.com
Thanks for the suggestion for that specific case. I'll definitely use that!

It doesn't, however, address the general case. I have images saved at different URLs that aren't tied to a File field (eg. Resized versions). So the question remains: are the docs clear enough to explain how to do this? 

--
You received this message because you are subscribed to a topic in the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-developers/T1d3HBXrol4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/d355974a-931f-425e-a0b7-5f08e7c808b6n%40googlegroups.com.

Tim Graham

unread,
Jan 30, 2021, 7:06:55 PM1/30/21
to Django developers (Contributions to Django itself)
I have no problem with your solution, or use MEDIA_URL as described at https://docs.djangoproject.com/en/dev/ref/settings/#media-url.

Generally for resized versions I'd use a third-party solution that offers some helpers like https://sorl-thumbnail.readthedocs.io/en/latest/examples.html#template-examples.

Adam Johnson

unread,
Jan 30, 2021, 7:08:01 PM1/30/21
to django-d...@googlegroups.com
There's also the media context processor that will inject MEDIA_URL into every template context, which is very similar to how get_media_prefix works: https://docs.djangoproject.com/en/3.1/ref/templates/api/#django-template-context-processors-media

You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAJtk5%2BsgizzDQYZhnJNGStjzkekqvTjqGk4fU8GKj_yt_%3Df9qQ%40mail.gmail.com.


--
Adam

mjnich...@gmail.com

unread,
Jan 31, 2021, 10:31:53 AM1/31/21
to Django developers (Contributions to Django itself)
Thanks - that's kind of exactly what I was looking for. 

I genuinely think the docs could be improved by mentioning these solutions on the main static files page :  https://docs.djangoproject.com/en/3.1/howto/static-files/  - as somebody trying to do it for the first time, I spent a lot of time searching the docs, didn't find that context processor, and eventually found the solution I used on StackOverflow. 

ANyway, just my 2c worth as a more inexperienced django person who is quite reliant on the docs :)
Reply all
Reply to author
Forward
0 new messages