Last Updated: February 25, 2016
·
8.608K
· 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