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

django, django-1.4, django-staticfiles, python

Solution

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

Problem

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!

Original source

Related problems