Простой вид для отображения/рендеринга статического изображения в Django

Я пытаюсь найти наиболее эффективный способ отображения изображения с помощью загрузчика контекста шаблона django. У меня есть статический каталог в моем приложении, который содержит изображение «victoryDance.gif» и пустой статический корневой каталог на уровне проекта (сsettings.py). при условии, что пути в моих файлах urls.pyи settings.pyверны. какой лучший вид?

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

спасибо за ответы Эти представления все работают!

7
задан JoshuaBox 21 July 2012 в 07:45
поделиться