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

-- 
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-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/d74329ab-1781-4769-be1c-5448431acab9n%40googlegroups.com.

Reply via email to