26👍
models.ImageField
has a property called url
which gives you the browsable path of your image.
You should be using artist.artist_image.url
instead of prepending MEDIA_URL
to your image name manually:
<img class="avatar secondary-content" src="{{artist.artist_image.url}}" />
—
Make sure artist_image
is not None
, otherwise calling .url
throws an exception.
33👍
You need to define the django.template.context_processors.media
template context processor in your settings for MEDIA_URL
variable to be present in the template context.
If this processor is enabled, every
RequestContext
will contain a
variableMEDIA_URL
, providing the value of theMEDIA_URL
setting.
Including this in your settings
will include MEDIA_URL
in the template context. It is not included by default in Django 1.8 settings. We need to set it explicitly.
context_processors = [
...
'django.template.context_processors.media', # set this explicitly
]
- Does Django use processes or threads to handle user requests in view?
- Separating Django App Views
- Different sessions for admin and applications in Django
0👍
You need to define this in settings
so you can put {{MEDIA_URL}} in templates
context_processors = [
'django.template.context_processors.media', # set this explicitly
]
- Django sub-applications & module structure
- Advanced Django Template Logic
- Adding SSL certificates to a mysql docker container