Do I need to learn a new frontend framework for
Unicorn gives you some magical template tags and HTML attributes to sprinkle in normal Django HTML templates. The backend code is a simple class that ultimately derives from
TemplateView. Keep using the same Django HTML templates, template tags, filters, etc and the best-in-class Django ORM without learning another new framework of the week.
Do I need to build an entire API to use
Django REST framework is pretty magical on its own, and if you will need a mobile app or other use for a REST API, it’s a great set of abstractions to follow REST best practices. But, it can be challenging implementing a robust API even with
Django REST framework. And I wouldn’t even attempt to build an API up from scratch unless it was extremely limited.
Do I need to need to install GraphQL to use
Nope! GraphQL looks like an awesome technology for specific use-cases and solves some pain points around creating a RESTful API. But, it is another peiece of technology to wrestle with.
Do I need to run an annoying separate node.js process or learn any tedious Webpack configuration incantations to use
Unicorn installs just like any normal Django package and is seamless to implement. There are a few “magic” attributes to sprinkle into a Django HTML template, but other than that it’s just like building a regular server-side application.
Does this replace Vue.js or React?¶
Nope! In some cases, you might need to actually build an SPA in which case
Unicorn really isn’t that helpful. In that case you might have to invest the time to learn a more involved frontend framework. Read Using VueJS alongside Django for one approach, or check out other articles about this.
Isn’t calling an AJAX endpoint on every input slow?¶
Unicorn is ideal for when an AJAX call would already be required (such as hitting an API for typeahead search or update data in a database). If that isn’t required, the lazy and debounce modifiers can also be used to prevent an AJAX call on every change.
But, what about security?¶
Unicorn follows the best practices of Django and requires a CSRF token to be set on any page that has a component. This ensures that no nefarious AJAX POSTs can be executed.
Unicorn also creates a unique component checksum with the Django secret key on every data change which also ensures that all updates are valid.
What browsers does
Unicorn mostly targets modern browsers, but any PRs to help support legacy browsers would be appreciated.
Unicorn is released?¶
Unicorn works great using whitenoise to serve static assets with a filename based on a hash of the file. CompressedManifestStaticFilesStorage works great for this purpose and is used by django-unicorn.com for this very purpose. Example code can be found at https://github.com/adamghill/django-unicorn.com/.
What is the difference between
Unicorn and lighter front-end frameworks like
htmx and alpine.js are great libraries to provide interactivity to your HTML. Both of those libraries are generalized front-end framework that you could use with any server-side framework (or just regular HTML). They are both well-supported, battle-tested, and answers to how they work are probably Google-able (or on Stackoverflow).
Unicorn isn’t in the same league as either
alpine.js. But, the benefit of
Unicorn is that it is tightly integrated with Django and it should “feel” like an extension of the core Django experience. For example:
redirecting from an action uses the Django
validation uses Django forms
Django Models are tightly integrated
Django messages “just work” the way you would expect them to
you won’t have to create extra URLs/views for AJAX calls to send back HTML because
Unicornhandles all of that for you