A Simple View to Display/Render a Static image in Django

天大地大妈咪最大 提交于 2020-01-22 19:43:07

问题


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!


回答1:


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:

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")

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")

Also there's a better way of doing things!

Else, if you need a efficient template engine use Jinja2

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',
)



回答2:


In your last example (image5) you should use {{ STATIC_PREFIX }} instead of {% STATIC_PREFIX %}

STATIC_PREFIX is variable, not a tag




回答3:


To avoid defining STATIC_URL explicitly, you can use a RequestContext when rendering your template. Just make sure django.core.context_processors.static is in your TEMPLATE_CONTEXT_PROCESSORS setting.

from django.template import RequestContext
...
return HttpResponse(html.render(RequestContext(request, ctx)))

Alternatively, you could use the static template tag.

html = Template('<img src="{% static "victoryDance.gif" %} alt="Hi!" />')

A third option is the get_static_prefix template tag.



来源:https://stackoverflow.com/questions/11577681/a-simple-view-to-display-render-a-static-image-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!