Django - Mascara CPF, DDD and phone number

Asked

Viewed 5,877 times

2

I need to put a mask on the number, ID number and phone number. I am a beginner in Python and Django and would like to know how I can present this mask at the time of typing:

For CPF: 999.999.999-99.

For telephone: (99) 99999-9999.

I created only the models.py and admin.py files and as magic I already have functional forms, but I want to improve the presentation by putting the mask.

What I have to do?

Where do I put this instruction?

I need an example if possible.

py.models

class Cliente(models.Model):

    SEXO_CHOICES = (
        ('M', u'Masculino'),
        ('F', u'Feminino'),
    )

    ESTADO_CIVIL_CHOICES = (
        ('S', u'Solteiro'),
        ('C', u'Casado'),
        ('D', u'Divorciado'),
        ('V', u'Viúvo'),
    )

    nome = models.CharField(max_length=60)
    cpf = models.CharField(max_length=11, blank=True, null=True)
    dtNascimento = models.DateField(blank=True, null=True, verbose_name='Data de nascimento')
    sexo = models.CharField(max_length=1, choices=SEXO_CHOICES)
    estado_civil = models.CharField(max_length=1, choices=ESTADO_CIVIL_CHOICES, verbose_name='Estado civil')
    nrTelCelular = models.CharField(max_length=11, blank=True, null=True, verbose_name='Nº telefone celular')
    nrTelFixo = models.CharField(max_length=11, blank=True, null=True, verbose_name='Nº telefone fixo')

    def __str__(self):
        return self.nome

admin py.

class ClienteAdmin(admin.ModelAdmin):
    model = Cliente
    list_display = ['nome','cpf', 'dtNascimento', 'sexo',
                    'estado_civil', 'nrTelCelular', 'nrTelFixo']
    list_filter = ['sexo', 'estado_civil']
    search_fields = ['nome']
admin.site.register(Cliente, ClienteAdmin)
Show 2 more comments

1 answer

4

you can use the Django-input-Mask

inside your form you can easily create your masks.

from input_mask.widgets import InputMask

class MyCustomInput(InputMask):
   mask = {'cpf': '000.000.000-00'}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.