Hello,
i have a created a custom filter to change the output of field shown in a template:
@register.filter(is_safe=True)
@stringfilter
def convert_type(value):
"""Convert name in database to template format"""
if value == CongePaye.choix[0][0]:
type = CongePaye.nom_element
elif value == CongeNonPaye.choix[0][0]:
type = CongeNonPaye.nom_element
elif value == CongeRecuperation.choix[0][0]:
type = CongeRecuperation.nom_element
elif type in TypeConge.objects.all().name:
type = value.capitalize()
else:
type = value.capitalize()
return type
when i apply the filter it gives me the error:
local variable 'type' referenced before assignment, in line 106
104 </td>
105 <td>{{ c.demandeur }}</td>
106 <td>{{ c.type | convert_type}}</td>
107 <td>{{ c.date_creation|date:"d/m/Y" }}</td>
108 <td>{{ c.status | convert_status }}</td>
109 <td>{{ c.start_date }}</td>
110 <td>{{c.demiJ_debut | convert_debut_fin}}</td>
111
112 <td>{{ c.end_date }}</td>
113 <td>{{c.demiJ_fin | convert_debut_fin}}</td>
114 <td>pp</td>
when i remove the “convert_type” filter the template works fine. and the weird is that when i apply it works for the other models where the field type is not forgeinkey.
this is the models for the classes:
class CongePaye(Conge):
choix = [("paye","Congé Payé")]
type = models.CharField(max_length=25, choices=choix, default=choix[0], verbose_name="Type")
class CongeNonPaye(Conge):
choix = [("nonpaye","Non Payé")]
type = models.CharField(max_length=25, choices=choix, default=choix[0], verbose_name="Type")
class CongeExceptionnel(Demande):
type = models.ForeignKey(TypeConge, on_delete=models.SET_NULL, null=True)
i apologize if i make a mistake in the post this is my first Topic in this forum.