django media url tag

Daniel Miliński picture Daniel Miliński · Feb 9, 2016 · Viewed 31.3k times · Source

Does django have media tag similar to static and url and how to setup it?

{% static 'styles/boo.css' %}
{% url 'some_app:some_name' %} 

Is this possible: {% media 'what here' %}?

How to setup it?

Answer

Leistungsabfall picture Leistungsabfall · Feb 9, 2016

There is no media template tag.

Having set MEDIA_ROOT and MEDIA_URL you can use a media file in a template by referring to its url attribute.

For example:

class Foo(models.Model):
    image = models.ImageField(
        ...
    )

and then in your template:

<img src="{{ foo_object.image.url }}">

Also, take a look at the docs about how to access media files.