Python django 媒体网址标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35288793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:14:35  来源:igfitidea点击:

django media url tag

pythondjangodjango-urls

提问by Daniel Miliński

Does django have mediatag similar to staticand urland how to setup it?

Django的是否有media标签类似static,并url和如何设置的呢?

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

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

How to setup it?

如何设置?

采纳答案by LostMyGlasses

You need {% get_media_prefix %}.

你需要{% get_media_prefix %}.

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

它设置的方式,说明在docs:你必须设置MEDIA_ROOTMEDIA_URL您的设置并添加MEDIA_URL到您的urls.py

回答by Leistungsabfall

There is no media template tag.

没有媒体模板标签。

Having set MEDIA_ROOTand MEDIA_URLyou can use a media file in a template by referring to its urlattribute.

设置后MEDIA_ROOTMEDIA_URL您可以通过引用其url属性在模板中使用媒体文件。

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.

另外,请查看有关如何访问媒体文件的文档

回答by elnygren

{% get_media_prefix %}and {{MEDIA_URL}} via context processorare both good alternatives for what you ask.

{% get_media_prefix %}{{MEDIA_URL}} 通过上下文处理器都是很好的选择。

That being said, if what you really want to achieve is to render a link to an uploaded media file such as an image, there is a better way.

话虽如此,如果您真正想要实现的是呈现指向上传的媒体文件(例如图像)的链接,那么还有更好的方法。

Model:

模型:

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

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

Template:

模板:

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

The reason for the @property is that you want to avoid cases where the ImageField doesn't contain an image. Accessing company.logo.urldirectly in the template will cause an exception in such a case.

@property 的原因是您希望避免 ImageField 不包含图像的情况。company.logo.url在这种情况下,直接在模板中访问会导致异常。

This is actually a long standing issue in Django: https://code.djangoproject.com/ticket/13327

这实际上是 Django 中一个长期存在的问题:https: //code.djangoproject.com/ticket/13327

回答by FirePower

For media files I use django-imagekit
Basic use:

对于我使用django-imagekit
基本使用的媒体文件:

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

models.py

模型.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

设置.py

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

    'imagekit',
]

.html

.html

{% load imagekit %}

{% thumbnail '100x50' source_file %}

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

阅读文档 - https://pypi.python.org/pypi/django-imagekit