I got an "Broken pipe" error when i try to submit info with form on django

Im workin on a local server, this is my error on console: [17/Sep/2024 09:54:26,384] - Broken pipe from ('127.0.0.1', 53046)

another errors:

int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

NOT NULL constraint failed: CAPER_factores.f1

When I try to send the form, even though I have all the factor inputs complete and have specified in the models that by default they adopt the value 0, it tells me that they have a null value or "NoneType"

these are my models:

class Personal(models.Model):
    T1 = 1
    T2 = 2
    T3 = 3
    T4 = 4

    TIPIFICACION_CHOICES = (
        (T1, 'Personal Policial'),
        (T2, 'Personal Civil Nivel 1'),
        (T3, 'Personal Civil Nivel 2'),
        (T4, 'Personal Civil Nivel 3'),
    )

    T1 = 1
    T2 = 2

    EDUCACION_CHOICES = (
        (T1, 'Educación Secundaria'),
        (T2, 'Educación Superior'),
    )

    legajo = models.IntegerField(unique=True)
    nombre = models.CharField(max_length=120, unique=True)
    apellido = models.CharField(max_length=120, unique=True)
    dni = models.IntegerField(unique=True)
    tipificacion = models.PositiveSmallIntegerField(choices=TIPIFICACION_CHOICES, default=T1)
    nivel_eduacativo = models.PositiveSmallIntegerField(choices=EDUCACION_CHOICES, default=T1)
    fecha_alta = models.DateField()
    posee_sancion = models.BooleanField()
    apto_ascenso = models.BooleanField()
    se_evalua = models.BooleanField()
    calificador = models.BooleanField()

    class Meta:
        ordering = ['apellido', 'nombre']
        verbose_name = "Personal"
        verbose_name_plural = "Personal"

    def __str__(self):
        return f"{self.apellido}, {self.nombre}"

    def get_absolute_url(self):
        """
        Devuelve la url para acceder a una instancia particular.
        """
        return reverse('personal_update', args=[str(self.pk)])
    
    def is_civil(self):
        """
        Devuelve un valor booleano para comprobar si la instancia del mismo
        modelo es civil.
        """
        return self.tipificacion in [self.T2, self.T3, self.T4]
    
    def is_policial(self):
        """
        Devuelve un valor booleano para comprobar si la instancia del mismo
        modelo es policial
        """
        return self.tipificacion == self.T1


class Calificaciones(models.Model):
    personal = models.ForeignKey(Personal, on_delete=models.PROTECT, null=True)
    periodo_desde = models.DateField(blank=True)
    periodo_hasta = models.DateField(blank=True)
    puntaje = models.IntegerField(null=True, blank=True)
    confirmado = models.BooleanField()
    impreso = models.BooleanField()
    pdf = models.BooleanField()
    periodo_anio = models.IntegerField(unique=False)

    class Meta:
        ordering = ['periodo_desde']
        verbose_name = "Calificación"
        verbose_name_plural = "Calificaciones"

    def __str__(self) -> str:
        return f'{self.id}° de {self.personal.nombre} {self.personal.apellido}'


class Factores(models.Model):
    personal = models.ForeignKey(Personal, on_delete=models.PROTECT, null=True)
    calificaciones = models.ForeignKey(Calificaciones, on_delete=models.PROTECT, null=True)
    f1 = models.IntegerField(default=0, blank=True)
    f2 = models.IntegerField(default=0, blank=True)
    f3 = models.IntegerField(default=0, blank=True)
    f4 = models.IntegerField(default=0, blank=True)
    f5 = models.IntegerField(default=0, blank=True)
    f6 = models.IntegerField(default=0, blank=True)
    f7 = models.IntegerField(default=0, blank=True)
    f8 = models.IntegerField(default=0, blank=True)
    f9 = models.IntegerField(default=0, blank=True)
    f10 = models.IntegerField(default=0, blank=True)
    f11 = models.IntegerField(default=0, blank=True)
    f12 = models.IntegerField(default=0, blank=True)
    f13 = models.IntegerField(default=0, blank=True)
    f14 = models.IntegerField(default=0, blank=True)

    class Meta:
        ordering = ['personal']
        verbose_name = "Factor"
        verbose_name_plural = "Factores"



class Fundamentos(models.Model):
    personal = models.ForeignKey(Personal, on_delete=models.PROTECT, null=True)
    calificaciones = models.ForeignKey(Calificaciones, on_delete=models.PROTECT, null=True)
    f1 = models.TextField(blank=True)
    f2 = models.TextField(blank=True)
    f3 = models.TextField(blank=True)
    f4 = models.TextField(blank=True)
    f5 = models.TextField(blank=True)
    f6 = models.TextField(blank=True)
    f7 = models.TextField(blank=True)
    f8 = models.TextField(blank=True)
    f9 = models.TextField(blank=True)
    f10 = models.TextField(blank=True)
    f11 = models.TextField(blank=True)
    f12 = models.TextField(blank=True)
    f13 = models.TextField(blank=True)
    f14 = models.TextField(blank=True)

    class Meta:
        ordering = ['personal']
        verbose_name = "Fundamento"
        verbose_name_plural = "Fundamentos"

these are my forms:

class CalificacionesForm(forms.ModelForm):
    class Meta:
        model = Calificaciones
        fields = ['periodo_desde', 'periodo_hasta', 'confirmado', 'impreso', 'pdf', 'periodo_anio']
        widgets = {
            'periodo_desde': forms.DateInput(attrs={'type': 'date'}),
            'periodo_hasta': forms.DateInput(attrs={'type': 'date'}),
            'puntaje': forms.HiddenInput(),
            'personal': forms.HiddenInput(),
        }


class FactoresForm(forms.ModelForm):
    class Meta:
        model = Factores
        fields = ['f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10', 'f11', 'f12', 'f13', 'f14']
        widgets = {
            'personal': forms.HiddenInput(),
            'calificaciones': forms.HiddenInput(),
        }

class FundamentosForm(forms.ModelForm):
    class Meta:
        model = Fundamentos
        fields = ['f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10', 'f11', 'f12', 'f13', 'f14']
        widgets = {
            'personal': forms.HiddenInput(),
            'calificaciones': forms.HiddenInput(),
            'f1': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
            'f2': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
            'f3': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
            'f4': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
            'f5': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
            'f6': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
            'f7': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
            'f8': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
            'f9': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
            'f10': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
            'f11': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
            'f12': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
            'f13': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
            'f14': forms.Textarea(attrs={'style': 'max-height: 1rem; resize: none;'}),
        }

this is my view for the forms:

def calificar(request, pk):
    template_name = 'calificar_form.html'
    personal = get_object_or_404(Personal, pk=pk)

    if request.method == 'POST':
        factores_form = FactoresForm(request.POST)
        fundamentos_form = FundamentosForm(request.POST)
        calificacion_form = CalificacionesForm(request.POST)

        if factores_form.is_valid() and fundamentos_form.is_valid() and calificacion_form.is_valid():

            total_factores = int(request.POST.get('total_factores', 0))

            personal_instance = personal
            calificacion_instance = calificacion_form.save(commit=False)
            factores_instance = factores_form.save(commit=False)
            fundamentos_instance = fundamentos_form.save(commit=False)

            calificacion_instance.puntaje = total_factores
            calificacion_instance.personal = personal_instance
            factores_instance.personal = personal_instance
            fundamentos_instance.personal = personal_instance
            factores_instance.calificaciones = calificacion_instance
            fundamentos_instance.calificaciones = calificacion_instance

            calificacion_instance.save()
            factores_instance.save()
            fundamentos_instance.save()

            return redirect('home')
    else:
        calificacion_form = CalificacionesForm(initial={'personal': personal})
        factores_form = FactoresForm(initial={'personal': personal})
        fundamentos_form = FundamentosForm(initial={'personal': personal})

    context = {
        'personal': personal,
        'fundamentos_form': fundamentos_form,
        'factores_form': factores_form,
        'calificacion_form': calificacion_form,
    }

    return render(request, template_name, context)

this is my template 'calificar':

{% extends 'base.html' %}

{% block content %}
{% load crispy_forms_tags %}

<div>
    <br />
    <h2><b>Calificar</b></h2>
    <br />
    <div class="contenedor-formulario">
        <form method="POST">
            {% csrf_token %}

            <div class="formularios">
                <div class="personal">
                    <h3><b>Personal</b></h3>
                    {{ calificacion_form | crispy }}
                </div>

                <div class="factores">
                    <h3><b>Factores</b></h3>
                    {{ factores_form | crispy }}
                </div>

                <div class="fundamentos">
                    <h3><b>Fundamentos</b></h3>
                    {{ fundamentos_form | crispy }}
                </div>
                
            </div>

            <h3 id="totalFactoresDisplay">Puntaje: 0</h3>

            <input type="hidden" name="total_factores" id="totalFactoresInput" />

            <div class="form_buttons">
                <button type="submit" class='btn btn-primary px-5 btn-calificar'>Calificar</button>
                <a href="{% url 'home' %}" class='btn btn-danger px-5 btn-calificar'>Cancelar</a>
            </div>
        </form>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
    const form = document.querySelector('form');
    const totalField = document.getElementById('totalFactoresInput');
    const factorInputs = form.querySelectorAll('input[name^="f"]');
    const totalFactoresDisplay = document.getElementById('totalFactoresDisplay');

    function calculateTotal() {
        let total = 0;
        factorInputs.forEach(input => {
            total += parseInt(input.value) || 0;
        });
        totalField.value = total;
        totalFactoresDisplay.textContent = `Total Factores: ${total}`;
    }

    factorInputs.forEach(input => {
        input.addEventListener('input', calculateTotal);
    });

    calculateTotal();
});
</script>
{% endblock %}

And these are my urls:

from django.urls import path
from .views import *

urlpatterns = [
    path('', ingreso_calificador, name='ingreso'),
    path('inicio', home, name='home'),
    **path('calificar/<int:pk>', calificar, name='calificar'),**
    path('inicio/deficiente', deficiente, name='deficiente'),
    path('inicio/bueno', bueno, name='bueno'),
    path('inicio/destacado', destacado, name='destacado'),
    path('inicio/muy-destacado', muy_destacado, name='muy-destacado'),
    path('inicio/ne', ne, name='ne'),
    path('inicio/sanciones', sanciones, name='sanciones'),
    path('inicio/confirmado', confirmado, name='confirmado'),
    path('inicio/sin-confirmar', confirmado, name='sin-confirmar'),
    path('inicio/oficiales', oficiales, name='oficiales'),
    path('inicio/civiles', civiles, name='civiles'),
    path('usuarios', usuarios, name='usuarios'),
    path('usuarios-auditorias', usuarios_auditorias, name='usuarios-auditorias'),
    path('roles', roles, name='roles'),
    path('audits', auditorias, name='auditorias'),
    path('auditorias/busquedasLU', auditorias_lu, name='auditorias-lu'),
    path('auditorias/busquedasLU/1', auditorias_lu_usuarios, name='auditorias-lu-usuarios'),
]

Im expecting that when i confirm the content in the form, that redirect me to 'home' template, but when i press de button, dont redirect me and dont save in db, it says me that is a problem in the server. I tried to simplify the code but did't work

Back to Top