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
django media url tag
提问by Daniel Miliński
Does django have media
tag similar to static
and url
and 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 %}
.
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
.
它设置的方式,说明在docs:你必须设置MEDIA_ROOT
与MEDIA_URL
您的设置并添加MEDIA_URL
到您的urls.py
。
回答by Leistungsabfall
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.
设置后MEDIA_ROOT
,MEDIA_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.url
directly 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