Skip to content

xshapira/dj-svg

Repository files navigation

dj-svg

PyPI version PyPI Supported Python Versions PyPI Supported Django Versions GitHub Actions (Code quality and tests)

dj-svg is a fork of django-inline-svg which works with Django >4, it's a simple plugin that adds an svg template tag to inline your SVGs in your Django templates.

Installation

Install it from pypi.

pip install dj-svg

Add dj_svg to your INSTALLED_APPS.

    INSTALLED_APPS = (
        ...
        'dj_svg',
        ...
    )

Usage

Store your SVGs in folder named svg at the root of any of your static file directories.

    my_app
    |-- static
    |   |-- svg
    |       |-- logo.svg
    |       |-- check.svg
    |       |-- cross.svg

Use the svg template tag.

    {% load svg %}

    <h1 class="logo">{% svg 'logo' class="css-class" height="16" width="16" %}</h1>

You can set SVG_DIRS to control where to look for your svgs.

    # settings.py

    SVG_DIRS=[
        os.path.join(BASE_DIR, 'my-svgs')
    ]

Passing html attributes to svg templatetag

With inlining SVG's, it's helpful to give them class names so you can change the fill color using CSS. And if you're using a framework like tailwind, you can do it like so:

{% svg 'icon-search' class='h-10 mt-6' %}

This will add fixed height and top margin to SVG.

Thanks to Jure Cuhalev for this one.

Support

The tests are run against Django 4.0 to 5.0.4 on Python 3.8 to 3.12.

License

MIT