Last Updated: February 25, 2016
·
9.067K
· caioariede

Add masked inputs to your Django forms

This can be well done using django-input-mask

Just:

pip install git+http://github.com/codasus/django-input-mask.git#egg=input_mask

Add input_mask to your INSTALLED_APPS

And start masking:

...
from input_mask.contrib.localflavor.us.widgets import (
    USDecimalInput,
)

class ExampleForm(forms.ModelForm):
    class Meta:
        model = Example
        widgets = {
            'value': USDecimalInput,
        }

You can also create your own custom masks just inheriting input_mask.widgets.InputMask

from input_mask.widgets import InputMask

class MyCustomInput(InputMask):
    mask = {
        'mask': '(99) 9999-9999',
    }

Remember that your templates must include the {{ form.media }}

More docs: https://github.com/codasus/django-input-mask

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside