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 :) On Sunday, 31 January 2021 at 01:08:01 UTC+1 Adam Johnson wrote: > 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 > > On Sat, 30 Jan 2021 at 22:46, Mike Nicholson <mjnich...@gmail.com> wrote: > >> 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? >> >> On Sat, 30 Jan 2021, 17:16 Tim Graham, <timog...@gmail.com> wrote: >> >>> 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 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 >>> >>> <https://groups.google.com/d/msgid/django-developers/d355974a-931f-425e-a0b7-5f08e7c808b6n%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- >> > 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 >> >> <https://groups.google.com/d/msgid/django-developers/CAJtk5%2BsgizzDQYZhnJNGStjzkekqvTjqGk4fU8GKj_yt_%3Df9qQ%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > Adam > -- 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/8c5350e0-46fb-4d7f-8692-27a9dbf4f04an%40googlegroups.com.