title: How can I efficiently render an image in Django?
body: <p>I am writing a small django app to handle images.
  I would like to make this as DRY as possible, so this is my goal:</p>
  
  <p>The user uploads a file, and inputs some fields like caption, alt text, title, etc..</p>
  
  <p>On the template, I would like to be able to do something like:</p>
  
  <pre><code>model.image.render_tag
  </code></pre>
  
  <p>which would output something like:</p>
  
  <pre><code>&lt;img src='path/image.jpg' title='title' alt_text='alt_text..' /&gt;
  </code></pre>
  
  <p>The problem is that that line should be on a template, so that if the developer needs to add some things or remove others, they can just edit the template, and the rendering will change in all of the rendered images.</p>
  
  <p>One could also add a second template and render that one.</p>
  
  <p>Now, there are a couple of methods to achieve this:</p>
  
  <h1>1)</h1>
  
  <p>Just include a template which renders a namespaced variable, and force the developer to 'change' the image variable name everytime, the template would be something like:</p>
  
  <pre><code>&lt;img src="{{ namespaced_image.raw.path }}" alt_text="{{ namespaced_image.alt_text }}"... /&gt;
  </code></pre>
  
  <p>In this case, the developer should rename the image to its namespaced version before including the path.</p>
  
  <p>Something like:</p>
  
  <pre><code>{% with namespaced_image=image %}
      {% include images/template.html %}
  {% end with %}
  </code></pre>
  
  <h1>2)</h1>
  
  <p>Add a template tag to render the image, based on a raw string that the user can edit in the settings, something like</p>
  
  <p><em>settings.py</em></p>
  
  <pre><code>namespaced_image_tag="&lt;img src={path} ... /&gt;"
  </code></pre>
  
  <p>and my template tag would do something like</p>
  
  <pre><code>@register
  def rendered_tag(image)
      return namespaced_image_tag.format(path=image.raw.path, ...)
  </code></pre>
  
  <p>So the developer could just add the custom tags file and use</p>
  
  <pre><code>{% load images_filters %}
  
  {{ image|rendered_tag }}
  </code></pre>
  
  <h1>3)</h1>
  
  <p>A mixture of the above:</p>
  
  <p>There would be a default template.html containing the tag, and a template tag that would render the template by giving it the namespaced image as context.</p>
  
  <pre><code>@register
  def rendered_tag(image, template="default_template.html")
      return render_template(template, namespaced_image=image)
  </code></pre>
  
  <p>So the developer could do something like</p>
  
  <pre><code>{% load images_filters %}
  {{ image|rendered_tag }}
  {{ image|rendered_tag:"custom_template_name" }}
  </code></pre>
  
  <p>I don't know however, how rendering several templates per request would affect performances.</p>
  
  <p>Also, I don't really like option #1 because it's not usable and readable.</p>
  
  <p>Option #2 is bad because I cannot use different "templates", and cannot extend others. I also don't like template tags very much, especially because the user is forced to import the file in every used template.</p>
  
  <p>Option #3 is the best in terms of usability, but I feel like it would affect a lot performances. Any tips?</p>