Note that there are some explanatory texts on larger screens.

plurals
  1. POA Simple View to Display/Render a Static image in Django
    primarykey
    data
    text
    <p>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 <code>settings.py</code>). assuming the paths within my <code>urls.py</code> and <code>settings.py</code> files are correct. what is the best view?</p> <pre><code>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('&lt;img src="{{ STATIC_URL }}victoryDance.gif" alt="Hi!" /&gt;') 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('&lt;img src="{{ STATIC_URL }}victoryDance.gif" alt="Hi!" /&gt;') 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 %}&lt;img src="{% static "victoryDance.gif" %}" /&gt;') return HttpResponse(html.render(Context(request))) def image4(request): # same pros as image3 html = Template('{% load static %} &lt;img src="{% get_static_prefix %}victoryDance.gif" %}" /&gt;') return HttpResponse(html.render(Context(request))) def image5(request): html = Template('{% load static %} {% get_static_prefix as STATIC_PREFIX %} &lt;img src="{{ STATIC_PREFIX }}victoryDance.gif" alt="Hi!" /&gt;') return HttpResponse(html.render(Context(request))) </code></pre> <p>thanks for answers These views all work!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload