在Django中显示/渲染静态图像的简单视图 [英] A Simple View to Display/Render a Static image in Django

查看:217
本文介绍了在Django中显示/渲染静态图像的简单视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到使用django的模板上下文加载器显示图像的最有效方法.我的应用程序中有一个静态目录,其中包含图像"victoryDance.gif"和一个在项目级别(带有settings.py)的空静态根目录.假设我的urls.pysettings.py文件中的路径正确.最好的景色是什么?

I am trying to find the most efficient way of displaying an image using django's template context loader. I have a static dir within my app which contains the image 'victoryDance.gif' and an empty static root dir at the project level (with settings.py). assuming the paths within my urls.py and settings.py files are correct. what is the best view?

from django.shortcuts import HttpResponse
from django.conf import settings
from django.template import RequestContext, Template, Context

def image1(request): #  good because only the required context is rendered
    html = Template('<img src="{{ STATIC_URL }}victoryDance.gif" alt="Hi!" />')
    ctx = { 'STATIC_URL':settings.STATIC_URL}
    return HttpResponse(html.render(Context(ctx)))

def image2(request): # good because you don't have to explicitly define STATIC_URL
    html = Template('<img src="{{ STATIC_URL }}victoryDance.gif" alt="Hi!" />')
    return HttpResponse(html.render(RequestContext(request)))

def image3(request): # This allows you to load STATIC_URL selectively from the template end
    html = Template('{% load static %}<img src="{% static "victoryDance.gif" %}" />')
    return HttpResponse(html.render(Context(request)))

def image4(request): # same pros as image3
    html = Template('{% load static %} <img src="{% get_static_prefix %}victoryDance.gif" %}" />')
    return HttpResponse(html.render(Context(request)))

def image5(request):
    html = Template('{% load static %} {% get_static_prefix as STATIC_PREFIX %} <img  src="{{ STATIC_PREFIX }}victoryDance.gif" alt="Hi!" />')
    return HttpResponse(html.render(Context(request)))

感谢您的回答这些观点都奏效!

thanks for answers These views all work!

推荐答案

如果您需要渲染图像,请在此处阅读 http://www.djangobook.com/en/1.0/chapter11/并使用您的以下代码版本:

If you need to render an image read a bit here http://www.djangobook.com/en/1.0/chapter11/ and use your version of the following code:

对于Django版本< = 1.5:

For django version <= 1.5:

from django.http import HttpResponse

def my_image(request):
    image_data = open("/path/to/my/image.png", "rb").read()
    return HttpResponse(image_data, mimetype="image/png")

对于django 1.5 +,mimetypecontent_type取代(很高兴我不再使用django):

For django 1.5+ mimetype was replaced by content_type(so happy I'm not working with django anymore):

from django.http import HttpResponse

def my_image(request):
    image_data = open("/path/to/my/image.png", "rb").read()
    return HttpResponse(image_data, content_type="image/png")

还有一种更好的方法做事!

否则,如果您需要高效的模板引擎,请使用Jinja2

Else, if you need a efficient template engine use Jinja2

否则,如果您使用的是Django的模板系统,则据我所知,您无需定义STATIC_URL,因为STATIC_URL由静态"上下文预处理器提供给模板:

Else, if you are using Django's templating system, from my knowledge you don't need to define STATIC_URL as it is served to your templates by the "static" context preprocessor:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.static',
    'django.core.context_processors.media',
    'django.core.context_processors.request',
    'django.contrib.messages.context_processors.messages',
)

这篇关于在Django中显示/渲染静态图像的简单视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆