Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 389
JoshuaBox Avatar asked Jul 20 '12 10:07

JoshuaBox


People also ask

How do I show images in Django?

How you specify the location of an image in Django is in between {% %}. In between these brackets, you specify static 'images\\Python. png', where Python is the image you want to display which is inside of the images directory in the static directory you create for the current app you are in.

What is the Render () function in Django?

render() Combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text. Django does not provide a shortcut function which returns a TemplateResponse because the constructor of TemplateResponse offers the same level of convenience as render() .

How static images are served in Django?

Configuring static files Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.


2 Answers

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',
)
like image 129
StefanNch Avatar answered Oct 14 '22 12:10

StefanNch


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

STATIC_PREFIX is variable, not a tag

like image 31
Igor Avatar answered Oct 14 '22 12:10

Igor