jayt
jayt

Reputation: 768

Django - {{ MEDIA_URL }} not working as expected

I'm having a slight problem where my Django media tag isn't working properly.

This doesn't return the path I expect - which would be '/media/uploads/image_name_goes_here/', and instead I get '/uploads/image_name_goes_here/' even though I have specified in my settings.py that the MEDIA_URL = '/media/'.

My upload_to path in the models for the Image is the following function..

def img_path(instance, filename):
    return ('uploads/%s' % (filename))

class Image(models.Model):
    ...
    image_url=models.ImageField(upload_to=img_path,null=True)
    ...

My html: <img src='{{ MEDIA_URL }}/{{ Image.img_path }}'/>

Any ideas on why the 'media/' bit of the URL does not show up?

Upvotes: 0

Views: 689

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599450

You don't have MEDIA_URL in the context of your template; it's not there by default.

Note however that you don't need it, and you shouldn't be accessing the field like this. Use the url attribute:

<img src='{{ Image.img_url.url }}'>

Upvotes: 1

Related Questions