Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django media url tag

Tags:

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?

like image 424
Daniel Miliński Avatar asked Feb 09 '16 09:02

Daniel Miliński


People also ask

How do I create a media URL in Django?

MEDIA_ROOT SettingCreate a new directory named media inside Django project root directory ( TGDB/django_project ), the same place where manage.py is located. Open settings.py file and add the following code to the end of the file, just below STATICFILES_DIRS which we had set earlier.

What is media URL in Django?

Open settings.py file of your project and add the following configuration. # Base url to serve media files. MEDIA_URL = '/media/'# Path where media is stored. MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') MEDIA_URL is the URL that will serve the media files.

How do I access Django media files?

Serving the files: If you are developing though, you can just get the django development server to serve them for you. To do this, you tell it to route all request that come in to http://example.com/media to your MEDIA_ROOT and all requests that come in to http://example.com/static to your STATIC_ROOT.

How do I use media images in Django?

The media directory should be created under myproject directory. If you want to put images and videos into separate folders then you can create images and videos directories under media directory.


2 Answers

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.

like image 123
Leistungsabfall Avatar answered Oct 21 '22 09:10

Leistungsabfall


You need {% get_media_prefix %}.

The way to set it up is explained in the docs: you have to set the MEDIA_ROOT and the MEDIA_URL in your settings and add the MEDIA_URL to your urls.py.

like image 40
LostMyGlasses Avatar answered Oct 21 '22 09:10

LostMyGlasses