0

I have a django application and in one of the templates I have something similar to:

<a href="/location/images/{{memb.EntryNr}}/">image</a>

This code is called multiple times - for each memb there is an associated .svg image that can be accessed with this url. Of course at the moment, there is just a link on the word 'image' to a separate page with the .svg.

What I want is to have the .svg's loaded into the template page instead of a link out. What is the easiest/best way to do this?

I am relatively new to Python/Django but I understand the basic concepts as well as HTML/CSS, however, I have zero experience with JavaScript.

EDIT: The .svg's are not stored in the filesystem. There is a separate view (separate to the main one for the template I'm working on here) that goes a bit like this:

def svg_image(request, entry_nr):
    svg_string = utils.DrawSVG.get_svg(entry_nr)
    return HttpResponse(svg_string)

I then have the url, which is accessed in the HTML template code above:

url(r'^images/(?P<entry_nr>[0-9]+)/$', views.svg_image, name='svg_image')
edc505
  • 871
  • 3
  • 8
  • 14
  • 1
    This has nothing to do with JS. Probably you are looking for [SVG in IMG](http://stackoverflow.com/questions/4476526/do-i-use-img-object-or-embed-for-svg-files). – Vidul Jul 27 '15 at 10:09

1 Answers1

5
{% load static %}
<p>
    <img src="/location/images/{{memb.EntryNr}}" width="200"/>
</p>

While rendering the django template you need to pass the content_type

def myview(request):
    svg_data = generate_some_svg_data()
    return HttpResponse(svg_data, content_type="image/svg+xml")
edc505
  • 871
  • 3
  • 8
  • 14
user3270602
  • 696
  • 7
  • 15
  • Thanks for your answer, it made me realise I had left out some crucial info, please see the EDIT. How should this approach be modified if I have a separate view for the ``.svg``'s? – edc505 Jul 27 '15 at 10:37
  • @edc505 in *no way at all*. The answer here *is* about the separate view that generates the SVG. – Daniel Roseman Jul 27 '15 at 10:44
  • @DanielRoseman Ok, great. Problem now is that if I try to access the svg image url, there is a TypeError: __init__() got an unexpected keyword argument 'mimetype' – edc505 Jul 27 '15 at 11:00
  • Actually if I use ``content_type`` instead of ``mimetype`` this appears to work: https://github.com/caffeinehit/django-oauth2-provider/pull/84 – edc505 Jul 27 '15 at 15:07