Skip to main content

django-svgicon

pypi

SVG icons for Django.

  • Automatic SVG Sprite generator based on icons used in the page
  • Read icons from directory and Python dict
  • Customizable template tags
  • In-memory cache

Requirements

  • Django >= 5
  • Python >= 3.10

Installation

pip install django-svgicon

To parse SVG files faster, install lxml [optional]:

pip install lxml

Configuration

Add svgicon to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    "svgicon",
    "YOUR_APP",
    ...
)

Add the middleware:

MIDDLEWARE = [
    ...
    "svgicon.middleware.SvgiconMiddleware",
    ...
]

Usage

  1. Put the icons in yourapp/static/svgicons, for example:
yourapp/static/svgicons/menu.svg
  1. Use it in your templates like this:
{% load svgicon %}
<!DOCTYPE html>
<html>
  <head>
    <title>The App</title>
    {% svgicon_style %} <!-- before your app.css -->
  </head>
  <body>
    {% svgicon_sprite %}

    <main>
      <p>Click on the {% icon "menu" %} button to open the menu.</p>
      <button>{% icon "menu" %}</button>
    </main>
  </body>
</html>

Rendered output:

<!DOCTYPE html>
<html>
  <head>
    <title>The App</title>
    <style>.svgicon{width:1em;height:1em;fill:currentColor;vertical-align:middle;}</style>
  </head>
  <body>
    <svg xmlns="http://www.w3.org/2000/svg" style="display:none;"><symbol id="icon-menu" viewbox="0 0 24 24"><path d="M3,3V21H21V3" /></symbol></svg>
    <main>
      <p>Click on the <svg class="svgicon" aria-hidden="true"><use href="#icon-menu"></use></svg> button to open the menu.</p>
      <button><svg class="svgicon" aria-hidden="true"><use href="#icon-menu"></use></svg></button>
    </main>
  </body>
</html>

For more examples like changing size or using aria-label, follow the demo template.

Settings

# By default, svgicon searches for icons using Django's static finders, 
# unless you specify a directory like this
SVGICON_ICONS_DIR = BASE_DIR / "myicons"

# Load icons from dict
SVGICON_ICONS = dict(
    square=(
        '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">'
        '<path d="M3,3V21H21V3" />'
        "</svg>"
    ),
)

# svgicon uses '1em' width and height for icons, which are relative to the parent's
# font-size.
SVGICON_WIDTH = '1em'
SVGICON_HEIGHT = '1em'

# Default CSS class for each icon
SVGICON_CSS_CLASS = 'svgicon'

# SVG symbol id prefix
SVGICON_SYMBOL_PREFIX = 'icon-'

# svgicon template tag names
SVGICON_TAGS = dict(
    ICON="icon",
    SPRITE="svgicon_sprite",
    STYLE="svgicon_style",
)

To auto-load svgicon without mentioning {% load svgicon %}, modify template settings like this:

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            ...
            "builtins": ["svgicon.templatetags.svgicon"], # add this 
            ...
        },
    },
]

Metadata

Release files for django-svgicon 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-svgicon 0.1.2
File Size Uploaded
django_svgicon-0.1.2.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-svgicon 0.1.2
File Interpreter ABI Platform
django_svgicon-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 23.2 kB

Release files / django_svgicon-0.1.2.tar.gz

Download URL django_svgicon-0.1.2.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b7a306c6504c8f30e1951c80c70240a695e6abb5a38db7f17b857bf080cc0ed9
BLAKE2b-256 checksum
How to use checksums
77c69769eb19972dabf11377bbc6e96d296407b0827b34cb71c5cd9d10d54449
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / django_svgicon-0.1.2-py3-none-any.whl

Download URL django_svgicon-0.1.2-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bf134ab04aeef301cd8ff2c000472794dfb2dd2780a833833d1d0ac65d78c7f0
BLAKE2b-256 checksum
How to use checksums
4e48f8e2ef478675a6ef5fbffa2762f2045289cd39763c59d174f9b22194dad3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page