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 sendcsrftoken value so{% csrf_token %}is not mandatory in the templates. This is added due the fact to additional page caching system likeVarnishdoes not operate effective ifCookievalue is present inVaryheader.CHECK_CHECKSUM_MATCH- default:True- If set toFalse,unicorndoes 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
--buildflagpoetry publish --build -r myrepo -u <myrepouser> -p <myrepopass>
- Sync with main package, version
0.62.0
- Update
restore_from_cachein to handle cases where parent of cached component does not have it in the children. It can happen when parent adds children dynamically and parent restored from cache was cached by another process where component being restored wasn't present.
- Components are now cached with timeout. Added
COMPONENT_CACHE_TIMEOUT(Default:600) setting that is used as component cache timeout.
- Delete
json_tagfrom 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_MATCHsetting which is set by default to True. If turned off,unicorndoes 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_TOKENis set toTrue. - [views.init.py:message] - Added decorator
csrf_handlethat checksUSE_CSRF_TOKENsetting and based on boolean appliescsrf_protectorcsrf_exemptdecorator. - [templatetags/unicorn.py:unicorn_scripts] - Added
USE_CSRF_TOKENto return in order to use in templates - [templates/unicorn/scripts.html] - Translate
USE_CSRF_TOKENvalue intouseCsrfTokenjavascript variable and pass it toUnicorn.init - [static/unicorn/js/unicorn.js:init] - apply
useCsrfTokentoargsthat are used latter in component. - [static/unicorn/js/component.js:Component] - add
useCsrfTokento class instance in constructor - [static/unicorn/js/messageSender.js:send] - set
csrftoken to headers only ifuseCsrfTokenis 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):
form_class = TodoForm
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. 🤩
Unicornprogressively enhances a normal Django view, so the initial render is fast and great for SEO.Unicornbinds to the elements you specify and automatically makes AJAX calls when needed.Unicornseamlessly 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!
