Use heroicons in your Django and Jinja templates.

Related tags

Django heroicons
Overview

heroicons

https://img.shields.io/github/workflow/status/adamchainz/heroicons/CI/main?style=for-the-badge https://img.shields.io/codecov/c/github/adamchainz/heroicons/main?style=for-the-badge https://img.shields.io/pypi/v/heroicons.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

Use heroicons in your Django and Jinja templates.

Requirements

Python 3.6 to 3.9 supported.

Django 2.2 to 3.2 supported.


Are your tests slow? Check out my book Speed Up Your Django Tests which covers loads of best practices so you can write faster, more accurate tests.


Usage

The heroicons package supports both Django templates and Jinja2 templates. Follow the appropriate guide below.

Django templates

  1. Install with python -m pip install heroicons[django].

  2. Add to your INSTALLED_APPS:

    INSTALLED_APPS = [
        ...,
        'heroicons',
        ...,
    ]

Now in your templates you can load the template library with:

{% load heroicons %}

This provides two tags to render <svg> icons: heroicon_outline and heroicon_solid, corresponding to the two icon styles in the set. The tags take these arguments:

  • name, positional: the name of the icon to use. You can see the icon names on the heroicons.com grid.
  • size, keyword: an integer that will be used for the width and height attributes of the output <svg> tag. Defaults to the icons’ designed sizes: 24 for outline and 20 for solid.
  • Any number of keyword arguments. These will be added as HTML attributes to the output <svg> tag. Underscores in attribute names will be replaced with dashes, allowing you to define e.g. data- attributes.

For example, to render an outline “academic-cap” icon, at 48x48, with some extra CSS classes and a data attribute “controller”, you would write:

{% heroicon_outline "academic-cap" size=48 class="h-4 w-4 inline" data_controller="academia" %}

Jinja templates

  1. Install with python -m pip install heroicons[jinja].

  2. Adjust your Jinja Environment to add the two global functions heroicon_outline and heroicon_solid, imported from heroicons.jinja. For example:

    from heroicons.jinja import heroicon_outline, heroicon_solid
    from jinja2 import Environment
    
    env = Environment()
    env.globals.update(
        {
            "heroicon_outline": heroicon_outline,
            "heroicon_solid": heroicon_solid,
        }
    )

Now in your templates you can call those two functions, which render <svg> icons corresponding to the two icon styles in the set. The functions take these arguments:

  • name, positional: the name of the icon to use. You can see the icon names on the heroicons.com grid.
  • size, keyword: an integer that will be used for the width and height attributes of the output <svg> tag. Defaults to the icons’ designed sizes: 24 for outline and 20 for solid.
  • Any number of keyword arguments. These will be added as HTML attributes to the output <svg> tag. Underscores in attribute names will be replaced with dashes, allowing you to define e.g. data- attributes.

For example, to render an outline “academic-cap” icon, at 48x48, with some extra CSS classes and a data attribute “controller”, you would write:

{{ heroicon_outline("academic-cap", size=48, class="h-4 w-4 inline", data_controller="academia") %}
Comments
  • Ability to configure the stroke width on a per icon basis

    Ability to configure the stroke width on a per icon basis

    Hi,

    Thanks for making this (I'm using it with Jinja 2).

    I'm wondering if we can get an optional stroke_width parameter which defaults to 2 which is the same as Heroicons.

    Use case and examples

    One thing that I tend to do quite often is tweak the stroke-width of an icon. This lets you create thin or bold icons.

    Here's a few HTML examples:

    <div class="p-4 space-y-4">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-10 w-10" fill="none" viewBox="0 0 24 24" stroke="currentColor">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
      </svg>
    
      <svg xmlns="http://www.w3.org/2000/svg" class="h-10 w-10" fill="none" viewBox="0 0 24 24" stroke="currentColor">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
      </svg>
    
      <svg xmlns="http://www.w3.org/2000/svg" class="h-10 w-10" fill="none" viewBox="0 0 24 24" stroke="currentColor">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="4" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
      </svg>
    </div>
    

    Which produces:

    image

    The only difference is the stroke-width is being set to 2 (default), 1 and 4. Every other attribute is the same.

    You can play around with this example at: https://play.tailwindcss.com/625MHAeePO

    opened by nickjj 12
  • Update to version 2.0.0

    Update to version 2.0.0

    Description

    Version 2.0.0 of the icons was just released upstream: https://twitter.com/steveschoger/status/1562117153591107586 , https://github.com/tailwindlabs/heroicons/releases/tag/v2.0.0

    Time to update them here!

    There are now three sets to support: outline, solid, and mini.

    opened by adamchainz 5
  • Upgrade embedded icons to v2.0.11

    Upgrade embedded icons to v2.0.11

    For #141.

    There's a bit of final mile work to do, but this should be a reasonable starting point.

    A couple of notes:

    • Tests that check the rendered SVG data haven't been updated yet, so currently fail. This is primarily because I wasn't sure of the best methodology for updating them, short of copying the output from running the code myself into the test -- which to me isn't really a test.
    • Due to the addition of the new mini style, the archive structure is different. I wasn't sure whether the best approach was to remap the new filenames to match the format the code currently expects, or to change the code itself. I opted for the latter for the sake of speed. This means that the template tags for "outline" translates the style name to "24/outline", "solid" to "24/solid" and "mini" to "20/solid" prior to attempting to load from the archive.
    opened by AndrewIngram 4
  • Add support for use in Wagtail admin

    Add support for use in Wagtail admin

    I recently created an open-source package (wagtail-heroicons) to add the ability to use Heroicons throughout the Wagtail CMS admin, extracted from what I was doing on a private repository for my day job.

    While my package does what it needs to do for me, it is a bit limiting as you can only use the icons within the admin interface, not in any templates written for Wagtail pages. I have thought about adding that ability as I think it adds to the usefulness for the community but seeing as this package already exists and is much better written than my own, I wanted to see if there was interest in adding support for the Wagtail admin within this package.

    Thanks!

    opened by joshuadavidthomas 3
  • can i assign attribute d to the path?

    can i assign attribute d to the path?

    Description

    <svg class="flex-shrink-0 w-6 h-6 text-teal-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true">
       <path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" />
    </svg>
    

    This is what i want to replace with heroicons, but when I do this

    {% heroicon_outline "check" stroke_width="2" stroke="currentColor" aria_hidden="true" d="M5 13l4 4L19 7" size=24 class="flex-shrink-0 w-6 h-6 text-teal-500"  %}
    

    I get

    <svg fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true" width="24" height="24" d="M5 13l4 4L19 7" class="flex-shrink-0 w-6 h-6 text-teal-500">
      <path stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path>
    </svg>
    

    as you can see, the d inside path is different. Not sure why. How do I override it?

    opened by simkimsia 2
  • SVG sprite mechanism

    SVG sprite mechanism

    Description

    SVG sprites are a useful way of decreasing page size for repeated icons: https://daily-dev-tips.com/posts/svg-sprites/

    We could provide a way of defining these for heroicons. An initial tag could pull in a list of icons in a spritesheet <svg> element, then a second tag could use those.

    opened by adamchainz 0
Owner
Adam Johnson
🦄 @django technical board member 🇬🇧 @djangolondon co-organizer ✍ AWS/Django/Python Author and Consultant
Adam Johnson
This is a template tag project for django to calculate in templates , enjoy it

Calculator-Template-Django this is a template tag project for django to calculate in templates , enjoy it Get Started : 1 - Download Source Code 2 - M

null 1 Feb 1, 2022
Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project

Django URL Shortener Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project Install this package to your Dja

Rishav Sinha 4 Nov 18, 2021
django-quill-editor makes Quill.js easy to use on Django Forms and admin sites

django-quill-editor django-quill-editor makes Quill.js easy to use on Django Forms and admin sites No configuration required for static files! The ent

lhy 139 Dec 5, 2022
Tweak the form field rendering in templates, not in python-level form definitions. CSS classes and HTML attributes can be altered.

django-widget-tweaks Tweak the form field rendering in templates, not in python-level form definitions. Altering CSS classes and HTML attributes is su

Jazzband 1.8k Jan 2, 2023
Full control of form rendering in the templates.

django-floppyforms Full control of form rendering in the templates. Authors: Gregor Müllegger and many many contributors Original creator: Bruno Renié

Jazzband 811 Dec 1, 2022
Full control of form rendering in the templates.

django-floppyforms Full control of form rendering in the templates. Authors: Gregor Müllegger and many many contributors Original creator: Bruno Renié

Jazzband 811 Dec 1, 2022
Django-Audiofield is a simple app that allows Audio files upload, management and conversion to different audio format (mp3, wav & ogg), which also makes it easy to play audio files into your Django application.

Django-Audiofield Description: Django Audio Management Tools Maintainer: Areski Contributors: list of contributors Django-Audiofield is a simple app t

Areski Belaid 167 Nov 10, 2022
Use Database URLs in your Django Application.

DJ-Database-URL This simple Django utility allows you to utilize the 12factor inspired DATABASE_URL environment variable to configure your Django appl

Jacob Kaplan-Moss 1.3k Dec 30, 2022
Use webpack to generate your static bundles without django's staticfiles or opaque wrappers.

django-webpack-loader Use webpack to generate your static bundles without django's staticfiles or opaque wrappers. Django webpack loader consumes the

null 2.4k Dec 24, 2022
Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.

Django-environ django-environ allows you to use Twelve-factor methodology to configure your Django application with environment variables. import envi

Daniele Faraglia 2.7k Jan 7, 2023
Rosetta is a Django application that eases the translation process of your Django projects

Rosetta Rosetta is a Django application that facilitates the translation process of your Django projects. Because it doesn't export any models, Rosett

Marco Bonetti 909 Dec 26, 2022
pytest-django allows you to test your Django project/applications with the pytest testing tool.

pytest-django allows you to test your Django project/applications with the pytest testing tool.

pytest-dev 1.1k Dec 14, 2022
django-dashing is a customisable, modular dashboard application framework for Django to visualize interesting data about your project. Inspired in the dashboard framework Dashing

django-dashing django-dashing is a customisable, modular dashboard application framework for Django to visualize interesting data about your project.

talPor Solutions 703 Dec 22, 2022
A Django app to initialize Sentry client for your Django applications

Dj_sentry This Django application intialize Sentry SDK to your Django application. How to install You can install this packaging by using: pip install

Gandi 1 Dec 9, 2021
Get inside your stronghold and make all your Django views default login_required

Stronghold Get inside your stronghold and make all your Django views default login_required Stronghold is a very small and easy to use django app that

Mike Grouchy 384 Nov 23, 2022
DCM is a set of tools that helps you to keep your data in your Django Models consistent.

Django Consistency Model DCM is a set of tools that helps you to keep your data in your Django Models consistent. Motivation You have a lot of legacy

Occipital 59 Dec 21, 2022
Organize Django settings into multiple files and directories. Easily override and modify settings. Use wildcards and optional settings files.

Organize Django settings into multiple files and directories. Easily override and modify settings. Use wildcards in settings file paths and mark setti

Nikita Sobolev 940 Jan 3, 2023
Django project starter on steroids: quickly create a Django app AND generate source code for data models + REST/GraphQL APIs (the generated code is auto-linted and has 100% test coverage).

Create Django App ?? We're a Django project starter on steroids! One-line command to create a Django app with all the dependencies auto-installed AND

imagine.ai 68 Oct 19, 2022
A Django chatbot that is capable of doing math and searching Chinese poet online. Developed with django, channels, celery and redis.

Django Channels Websocket Chatbot A Django chatbot that is capable of doing math and searching Chinese poet online. Developed with django, channels, c

Yunbo Shi 8 Oct 28, 2022