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?
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.
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With