Direct View#

Usually components will be included in a regular Django template, however a component can also be specified in a file in instances where having an additional template is not necessary.

Template Requirements#

  • there must be one (and only one) element that wraps around the portion of the template that should be handled by Unicorn

  • the wrapping element must include unicorn:view as an attribute

  • the template must included the unicorn_scripts and csrf_token template tags

Similar to a class-based view, Unicorn components have a as_view function which is used in


from django_unicorn.components import UnicornView

class BookView(UnicornView):
    title = ""
<!-- book.html -->
{% load unicorn %}

    {% unicorn_scripts %}
    {% csrf_token %}

    <div unicorn:view>
      <input unicorn:model="title" type="text" /><br />
      {{ title }}
from django.urls import path
from import BookView

urlpatterns = [
    path("book", BookView.as_view(), name="book"),