Skip to main content

Cleave.js integration for Django forms

This re-usable app helps integrating Cleave.js into Django projects. Cleave.js is a JavaScript library that can “help you format input text content automatically”.

django-cleavejs eases integration into Django. It wraps Cleave.js’s options object into a form widget class, which takes care of transporting the options to JavaScript and initialising Cleave.js for each such input field.

Installation

To add django-cleavejs to a project, first add it as dependency to your project, e.g. using poetry:

$ poetry add django-cleavejs

Then, add it to your INSTALLED_APPS setting to make its static files available:

INSTALLED_APPS = [
    ...
    "dj_cleavejs.apps.DjCleaveJSConfig",
    ...
]

You need to make Cleave.js itself available by some means. You can download it manually, or use your favourite asset management library. For instance, you could use django-yarnpkg to depend on the cleave.js Yarn package:

YARN_INSTALLED_APPS = [
  "cleave.js",
]
NODE_MODULES_ROOT = os.path.join(BASE_DIR, "node_modules")
STATICFILES_FINDERS += ["django_yarnpkg.finders.NodeModulesFinder"]

You can then either include it in your template yourself, or make django-cleavejs add it to its form media by configuring the URL or path in your settings:

CLEAVE_JS = "cleave.js/dist/cleave.min.js"

The above example uses the path below STATIC_ROOT where django-yarnpkg would put the package, but you can of course use any URL or path you want depending on how you want to make Cleave.js available to the browser.

Usage

Once available, you can create a form as normal and use the widget on any CharField:

from django.forms import CharField, Form

from dj_cleavejs import CleaveWidget


class TestForm(Form):
    windows_xp_serial = CharField(widget=CleaveWidget(blocks=[5, 5, 5, 5, 5],
                            delimiter="-"))

In your template, make sure to include the form media of your form somewhere after the form.

The CleaveWidget supports all options Cleave.js supports, as described in its options documentation. In the future, django-cleavejs will also provide shortcuts.

Example

The source distribution as well as the Git repository contain a full example application that serves a test form unter /test.html.

It is reduced to a minimal working example for the reader’s convenience.

Metadata

Release files for django-cleavejs 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-cleavejs 0.1.1
File Size Uploaded
django-cleavejs-0.1.1.tar.gz 12.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-cleavejs 0.1.1
File Interpreter ABI Platform
django_cleavejs-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 24.7 kB

Release files / django-cleavejs-0.1.1.tar.gz

Download URL django-cleavejs-0.1.1.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
edbab0745da648f38f57a135a317d26168bb831196c890c5307680193531f976
BLAKE2b-256 checksum
How to use checksums
db6f04360b6a2e6798ba29a88680e566e88e6f47f04f4cd3ac6a5355ef7072bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.7 CPython/3.9.9 Linux/5.15.0-2-amd64

Release files / django_cleavejs-0.1.1-py3-none-any.whl

Download URL django_cleavejs-0.1.1-py3-none-any.whl
Size 12.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3097c00959f420a30f801d9c828e398270af90af3e26291810a239e36da48691
BLAKE2b-256 checksum
How to use checksums
3d4d65c623daec776fd09fbdb0ef59ae71100c472ea04e64989252baf331f7d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.7 CPython/3.9.9 Linux/5.15.0-2-amd64

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page