Validation of the fields in Django

0

I would like to modify some of the fields in my form to be validated but I do not know how. I know there are several methods but I do not understand them, if you can help me, thank you.

This is my forms.py :

from django import forms
from django.contrib.admin import widgets
from datos.models import Empleados
from django.forms import ModelForm, TextInput, Select, EmailInput


class EmpleadoForm(ModelForm):
class Meta:
    model = Empleados

    fields = [
        'nombre',
        'apellidos',
        'genero',
        'ci',
        'cargo',
        'email',
        'telefono',
        'documento',
        'direccion',
        'estado_civil',
        'grado_instruccion',
        'numero_de_hijos',

    ]
    widgets = {
        'nombre': TextInput(attrs={'class':'form-control','placeholder':'Introduzca nombre'}),
        'apellidos': TextInput(attrs={'class':'form-control','placeholder':'Introduzca apellidos'}),
        'genero': Select(attrs={'class':'form-control','placeholder':'Seleccione su género'}),
        'ci': TextInput(
            attrs={
                'class':'form-control',
                'placeholder':'Introduzca ci', 
                'minlength':'7',
                'maxlength':'8',}),
        'cargo': TextInput(attrs={'class':'form-control','placeholder':'Introduzca cargo'}),
        'email': EmailInput(attrs={'class':'form-control','placeholder':'Introduzca email'}),
        'telefono': TextInput(attrs={'class':'form-control','placeholder':'Introduzca solo numero de telefono'}),
        'direccion': TextInput(attrs={'class':'form-control','placeholder':'Introduzca su direccion'}),
        'estado_civil': Select(attrs={'class':'form-control','placeholder':'Estado civil'}),
        'grado_instruccion': Select(attrs={'class':'form-control','placeholder':'Grado de instruccion'}),
        'numero_de_hijos': TextInput(attrs={'class':'form-control','type':'number','placeholder':'Numero de hijos'}),
    }

The fields that I want to validate are the CI: (DNI) that only accepts numeric data, the email field so that it does not accept any mail only the valid ones, name and surnames that only accept letters, and any other that I suggest and I really apologize is that I do not understand much yet.

    
asked by Jhonny Barreto 05.09.2018 в 20:35
source

2 answers

1

so that you save some time and it is a bit easier to implement what you want you can do it like this:

  

Forms file.py

     

from django.forms import ModelForm from Datos.models import Empleados class FormEmpleados(ModelForm): class Meta: model = Empledos fields = "__all__"

     
    

File models.py      class Empleados(models.Model): nombre = models.CharField('Nombre', max_length=8, min_length=7, null=False, unique=True, blank=False, text_help) correo = models.EmailField()

  

that is, you just have to use the form, to validate what should go inside each field as expressions that meet a format, you can use javascript, let an example and send it to you

    
answered by 05.09.2018 / 21:39
source
1

create a file validacion.js , then within the same copy the following:

  

function validate_employee (form)

     

{

     

var ci = / ^ [0123456789] + $ /;

     

if (! carnet.test (form.id_ci.value) || form.id_ci.value.length! = 8)

     

{   form.id_ci.focus ();   return false;   }

     

}

then in the html where the form is this: form name="formulario" onsubmit="return validar_empleado(formulario)" ah do not forget to include the file validacion.js script type="text/javascript" src="{% static 'validacion.js '%}" with this, django validates the email and ci fields, and with the js it is verified that the entered data are not incorrect, I hope you understand and it will help you

    
answered by 05.09.2018 в 22:01