• Stars
    star
    1,634
  • Rank 28,618 (Top 0.6 %)
  • Language
    Python
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The magical reactive component framework for Django ✨

django-unicorn logo

Unicorn

The magical reactive component framework for Django ✨

PyPI PyPI - Downloads GitHub Sponsors All Contributors

Unicorn adds modern reactive component functionality to your Django templates without having to learn a new templating language or fight with complicated JavaScript frameworks. It seamlessly extends Django past its server-side framework roots without giving up all of its niceties or forcing you to re-building your application. With Django Unicorn, you can quickly and easily add rich front-end interactions to your templates, all while using the power of Django.

⚑ Getting started

1. Install the package

pip install django-unicorn OR poetry add django-unicorn

2. Add django_unicorn to INSTALLED_APPS

# settings.py
INSTALLED_APPS = (
    # other apps
    "django_unicorn",
)

3. Update urls.py

# urls.py
import django_unicorn

urlpatterns = (
    # other urls
    path("unicorn/", include("django_unicorn.urls")),
)

4. Add Unicorn to the HTML template

<!-- template.html -->
{% load unicorn %}

<html>
  <head>
    {% unicorn_scripts %}
  </head>
  <body>
    {% csrf_token %}
  </body>
</html>

5. Create a component

python manage.py startunicorn COMPONENT_NAME

Unicorn uses the term "component" to refer to a set of interactive functionality that can be put into templates. A component consists of a Django HTML template and a Python view class which contains the backend code. After running the management command, two new files will be created:

  • your_app/templates/unicorn/COMPONENT_NAME.html (component template)
  • your_app/components/COMPONENT_NAME.py (component view)

6. Add the component to your template

<!-- template.html -->
{% load unicorn %}

<html>
  <head>
    {% unicorn_scripts %}
  </head>
  <body>
    {% csrf_token %}

    {% unicorn 'COMPONENT_NAME' %}
  </body>
</html>

Example todo component

The unicorn: attributes bind the element to data and can also trigger methods by listening for events, e.g. click, input, keydown, etc.

<!-- ../templates/unicorn/todo.html -->

<div>
  <form unicorn:submit.prevent="add">
    <input type="text"
      unicorn:model.defer="task"
      unicorn:keyup.escape="task=''"
      placeholder="New task" id="task"></input>
  </form>
  <button unicorn:click="add">Add</button>
  <button unicorn:click="$reset">Clear all tasks</button>

  <p>
    {% if tasks %}
      <ul>
        {% for task in tasks %}
          <li>{{ task }}</li>
        {% endfor %}
      </ul>
    {% else %}
      No tasks πŸŽ‰
    {% endif %}
  </p>
</div>
# ../components/todo.py

from django_unicorn.components import UnicornView
from django import forms

class TodoForm(forms.Form):
    task = forms.CharField(min_length=2, max_length=20, required=True)

class TodoView(UnicornView):
    task = ""
    tasks = []

    def add(self):
        if self.is_valid():
            self.tasks.append(self.task)
            self.task = ""

✨ Wait, is this magic?

Sort of! At least it might feel like it. 🀩

  1. Unicorn progressively enhances a normal Django view, so the initial render is fast and great for SEO.
  2. Unicorn binds to the elements you specify and automatically makes AJAX calls when needed.
  3. Unicorn seamlessly updates the DOM when the HTML changes.

Focus on building regular Django templates and Python classes without needing to switch to another language or use unnecessary infrastructure.

🀯 But wait, there's more!

As if that wasn't enough, other features include:

πŸ“– Dig In

❀️ Support

This project is supported by GitHub Sponsors and Digital Ocean.

πŸ”§ Contributors

Check out this guide for more details on how to contribute.

Thanks to the following wonderful people (emoji key) who have helped build Unicorn.

Adam Hill
Adam Hill

πŸ’» ⚠️
Andres Vargas
Andres Vargas

πŸ’»
Eddy Ernesto del Valle Pino
Eddy Ernesto del Valle Pino

πŸ’»
Yaser Al-Najjar
Yaser Al-Najjar

πŸ’»
Stephan Traub
Stephan Traub

⚠️
Fredrik Borg
Fredrik Borg

πŸ’» ⚠️
mbacicc
mbacicc

πŸ’»
Ron
Ron

πŸ“–
Franziskhan
Franziskhan

πŸ’»
Josh Higgins
Josh Higgins

⚠️ πŸ’»
Amayas Messara
Amayas Messara

πŸ’»
Apoorva Pandey
Apoorva Pandey

⚠️ πŸ’»
Christian GonzΓ‘lez
Christian GonzΓ‘lez

πŸ’» πŸ“–
robwa
robwa

πŸ’» ⚠️
Preston Badeer
Preston Badeer

πŸ“–
Sergei
Sergei

πŸ“– πŸ’» ⚠️
bazubii
bazubii

πŸ’» ⚠️
Dan Caron
Dan Caron

πŸ“–
Shantanu
Shantanu

πŸ’»

This project follows the all-contributors specification. Contributions of any kind welcome!

More Repositories

1

coltrane

A simple content site framework that harnesses the power of Django without the hassle.
Python
67
star
2

django-fbv

Utilities to make function-based views cleaner, more efficient, and better tasting.
Python
59
star
3

exchanger

Query Exchange Web Services
JavaScript
45
star
4

minestrone

Search, modify, and parse messy HTML with ease.
Python
38
star
5

django-rich-logging

A prettier way to see Django requests while developing.
Python
36
star
6

fediview

Get a digest of popular posts and boosts from your Mastodon (fediverse) timeline.
Python
34
star
7

dlite

A tiny, blazing fast view library that creates reactive Web Components
JavaScript
26
star
8

djangocon-eu-2021-conference-talk

Code, slides, scripts and other notes for my DjangoCon EU 2021 talk.
Python
22
star
9

django-live-dashboard

Shows current requests to a Django application. Inspired by Phoenix's LiveDashboard.
HTML
9
star
10

dj

Run commands with `dj {command_name}`. Use aliases defined in a simple config file or defaults to Django management commands.
Python
8
star
11

unsuckjs.com

Progressively enhance HTML with lightweight JavaScript libraries.
Python
7
star
12

django-unicorn.com

Website for django-unicorn
HTML
6
star
13

python-utils.com

Utilities for Python
HTML
5
star
14

dj-angles

Add some more bracket angles to your Django templates 🎧
Python
4
star
15

magical-creatures

Example Django site built with Unicorn
Python
4
star
16

django-unicorn-playground

Prototype and debug `Unicorn` components.
Python
4
star
17

django-compressor-js

Javascript precompiler for django-compressor
Python
3
star
18

djajax

Helper utilities for Django + AJAX
Python
3
star
19

docker-python-poetry-django

Example project that demonstrates Docker, Python, Poetry, and Django
Dockerfile
3
star
20

sphinx-markdown-docs

An example of how to use Sphinx to create an awesome documentation site.
2
star
21

build-docker-and-deploy-to-caprover

2
star
22

isitwebscale

isitwebscale.adamghill.com
CSS
2
star
23

conference-notes

Conferences
2
star
24

ExchangeReader

Relatively easy API to get email from Outlook Web Access (WebDAV).
C#
2
star
25

openspace

openspace
JavaScript
2
star
26

dorkjs

The most important JavaScript framework ever created.
JavaScript
2
star
27

deploy-o-matic

Copies files from one directory to another
1
star
28

ni

A port of https://github.com/adamghill/dj to `nim`.
Nim
1
star