Django media url tag

Django has a tag mediasimilar to staticand url, and how to configure it?

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

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

How to configure it?

+4
source share
4 answers

You need to {% get_media_prefix %}.

Way to set it up is explained in the documents : you need to install MEDIA_ROOTand MEDIA_URLin its settings and add MEDIA_URLto your urls.py.

+6
source

No media template tag.

By setting MEDIA_ROOTand MEDIA_URL, you can use the media file in the template by specifying its attribute url.

For example:

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

and then in your template:

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

.

+12

{% get_media_prefix%} {{MEDIA_URL}} , .

, , , - , , .

:

class Company(models.Model):
    logo = models.ImageField()

    @property
    def logo_url(self):
        if self.logo and hasattr(self.logo, 'url'):
            return self.logo.url

:

<img src="{{company.logo_url}}"/>

@property , , ImageField . company.logo.url .

Django: https://code.djangoproject.com/ticket/13327

+1

django-imagekit
:

from django.db import models
from imagekit.models import ProcessedImageField
from imagekit.processors import ResizeToFill

models.py

class Photo(models.Model):
    owner = models.ForeignKey(Project, on_delete=models.CASCADE)
    photos = ProcessedImageField(upload_to='pagename/images',
                                       processors=[ResizeToFill(900, 600)],
                                       format='JPEG',
                                       options={'quality': 90})

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'imagekit',
]

.html

{% load imagekit %}

{% thumbnail '100x50' source_file %}

Read the documentation - https://pypi.python.org/pypi/django-imagekit

0
source

Source: https://habr.com/ru/post/1627930/


All Articles