Hi Mike,

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

https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.fields.files.FieldFile.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/.


On Saturday, January 30, 2021 at 10:56:36 AM UTC-5 mjnich...@gmail.com 
wrote:

> 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/d355974a-931f-425e-a0b7-5f08e7c808b6n%40googlegroups.com.

Reply via email to