This is a repository of a customized version of Django Unicorn package.
Loosened version of package is by default functionally the same as the official package. Extended stuff that were added are controllable with settings/feature flags.
This repository is intended to sync periodically with adamghill
repository in order to be up to date with the latest changes.
Releasing as an installable package is planned only for a private repo for now, and version numbers will follow an official one with one extra dotted number. In the example, if there is an official version of 0.50.0
, the tag that follows this version here will be 0.50.0.1
.
Available additional settings that can be set to UNICORN
dict in settings.py which are not part of official package.
USE_CSRF_TOKEN
- default:True
- If set toFalse
, unicorn does not check or sendcsrf
token value so{% csrf_token %}
is not mandatory in the templates. This is added due the fact to additional page caching system likeVarnish
does not operate effective ifCookie
value is present inVary
header.CHECK_CHECKSUM_MATCH
- default:True
- If set toFalse
,unicorn
does not perform data checksum check on each request.COMPONENT_CACHE_TIMEOUT
- default:600
- Time in seconds used to cache components.
-
Add your repository to poetry.config:
poetry config repositories.myrepo http://to.my.repo
-
Publish package to your repository with
--build
flagpoetry publish --build -r myrepo -u <myrepouser> -p <myrepopass>
- Components are now cached with timeout. Added
COMPONENT_CACHE_TIMEOUT
(Default:600
) setting that is used as component cache timeout.
- Delete
json_tag
from child components as it could causeRecursionError
- Avoid recursion upon caching parent/child complex components with
pickle.dumps
- Sync with main package, version
0.61.0
- No customizations, just sync with main package.
- add optional
CHECK_CHECKSUM_MATCH
setting which is set by default to True. If turned off,unicorn
does not perform data checksum check on each request.
- No customizations, just sync with main package.
- No customizations, just sync with main package.
- No customizations, just sync with main package.
- No customizations, just sync with main package.
- No customizations, just sync with main package.
- No customizations, just sync with main package.
- Add
USE_CSRF_TOKEN
(useCsrfToken
) setting that if set toFalse
(false
) avoids CSRF token check. By defaultUSE_CSRF_TOKEN
is set toTrue
. - [views.init.py:message] - Added decorator
csrf_handle
that checksUSE_CSRF_TOKEN
setting and based on boolean appliescsrf_protect
orcsrf_exempt
decorator. - [templatetags/unicorn.py:unicorn_scripts] - Added
USE_CSRF_TOKEN
to return in order to use in templates - [templates/unicorn/scripts.html] - Translate
USE_CSRF_TOKEN
value intouseCsrfToken
javascript variable and pass it toUnicorn.init
- [static/unicorn/js/unicorn.js:init] - apply
useCsrfToken
toargs
that are used latter in component. - [static/unicorn/js/component.js:Component] - add
useCsrfToken
to class instance in constructor - [static/unicorn/js/messageSender.js:send] - set
csrf
token to headers only ifuseCsrfToken
is set totrue
.
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 rebuild 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.
https://www.django-unicorn.com has extensive documentation, code examples, and more!
pip install django-unicorn
OR poetry add django-unicorn
# settings.py
INSTALLED_APPS = (
# other apps
"django_unicorn",
)
# urls.py
import django_unicorn
urlpatterns = (
# other urls
path("unicorn/", include("django_unicorn.urls")),
)
<!-- template.html -->
{% load unicorn %}
<html>
<head>
{% unicorn_scripts %}
</head>
<body>
{% csrf_token %}
</body>
</html>
python manage.py startunicorn myapp 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:
myapp/templates/unicorn/COMPONENT_NAME.html
(component template)myapp/components/COMPONENT_NAME.py
(component view)
<!-- template.html -->
{% load unicorn %}
<html>
<head>
{% unicorn_scripts %}
</head>
<body>
{% csrf_token %}
{% unicorn 'COMPONENT_NAME' %}
</body>
</html>
The unicorn:
attributes bind the element to data and can also trigger methods by listening for events, e.g. click
, input
, keydown
, etc.
<!-- 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>
# 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 = ""
Sort of! At least it might feel like it. 🤩
Unicorn
progressively enhances a normal Django view, so the initial render is fast and great for SEO.Unicorn
binds to the elements you specify and automatically makes AJAX calls when needed.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.
As if that wasn't enough, other features include:
- Form Validation
- Redirection
- Loading States
- Dirty States
- Partial Updates
- Polling
- Scroll Triggering
- Messages
- Javascript Integration
This project is supported by GitHub Sponsors and Digital Ocean.
Check out this guide for more details on how to contribute.
Thanks to the following wonderful people (emoji key) who have helped build Unicorn
.
This project follows the all-contributors specification. Contributions of any kind welcome!