Skip to main content

Cleave.js integration for Django forms

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-cleavejs-0.1.1.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

django_cleavejs-0.1.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file django-cleavejs-0.1.1.tar.gz.

File metadata

  • Download URL: django-cleavejs-0.1.1.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.9.9 Linux/5.15.0-2-amd64

File hashes

Hashes for django-cleavejs-0.1.1.tar.gz
Algorithm Hash digest
SHA256 edbab0745da648f38f57a135a317d26168bb831196c890c5307680193531f976
MD5 c214fefdec26199a618c5aabcd66e84e
BLAKE2b-256 db6f04360b6a2e6798ba29a88680e566e88e6f47f04f4cd3ac6a5355ef7072bb

See more details on using hashes here.

File details

Details for the file django_cleavejs-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: django_cleavejs-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.9.9 Linux/5.15.0-2-amd64

File hashes

Hashes for django_cleavejs-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3097c00959f420a30f801d9c828e398270af90af3e26291810a239e36da48691
MD5 ea8cfcd3609161e0c83f374095411afd
BLAKE2b-256 3d4d65c623daec776fd09fbdb0ef59ae71100c472ea04e64989252baf331f7d3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page