Installation

Install Unicorn

Install Unicorn the same as any other Python package (preferably into a virtual environment).

pip install django-unicorn

OR

poetry add django-unicorn

Note

If attempting to install django-unicorn and orjson is preventing the installation from succeeding, check whether it is using 32-bit Python. Unfortunately, orjson is only supported on 64-bit Python. More details in issue #105.

Integrate Unicorn with Django

1. Add the required apps to the INSTALLED_APPS list in the Django settings file (normally settings.py).

# settings.py
INSTALLED_APPS = (
    # other apps
    "django_unicorn",  # required for Django to register urls and templatetags
    # un-comment the next line once you have added your first Unicorn component
    # "unicorn",  # where components and templates are stored;
)

2. Add path("unicorn/", include("django_unicorn.urls")),into the project’surls.py.

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

3. Add {% load unicorn %} to the top of the Django HTML template.

4. Add {% unicorn_scripts %} into the Django HTML template and make sure there is a {% csrf_token %} in the template as well.

<!-- index.html -->
{% load unicorn %}
<html>
  <head>
    {% unicorn_scripts %}
  </head>
  <body>
    {% csrf_token %}
  </body>
</html>

Then, create a component.