Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django static templatetag not displaying SVG

I'm trying to use django's static templatetag to display an SVG, but it doesn't seem to recognize the SVG as a valid image url. This is what I currently have:

settings.py

import mimetypes
mimetypes.add_type("images/svg+xml", ".svg", True)

landing.html

{% load staticfiles %}
<img src="{% static 'images/right-arrow.svg' %}" />

At least in my view.py, it recognizes the SVG mimetype:

views.py

print(mimetypes.guess_type(static('images/right-arrow.svg'))) 
  # returns ('images/svg+xml', None)

The SVG does display in a non-django page, and it will download the SVG if I try to open the SVG path in a new browser tab.

I'm currently using python 3.4 and django 1.8.4.

like image 565
NJP Avatar asked Jan 19 '26 18:01

NJP


1 Answers

Add this in your settings.py file.

import mimetypes

mimetypes.add_type("image/svg+xml", ".svg", True)
mimetypes.add_type("image/svg+xml", ".svgz", True)

In your cases you have added images in add_type which should be singular (image).

like image 128
Umar Asghar Avatar answered Jan 21 '26 07:01

Umar Asghar