I'm applying a validation for the datepicker when the user selects a future date. So far, the form does not save an invalid entry but the problem is that it doesn't display the error message for the field. This is what I've tried so far:
forms.py
class PostPagos(forms.ModelForm):
def clean(self):
cleaned_data = super(PostPagos, self).clean()
fecha = cleaned_data.get('fecha')
hoy = datetime.date.today()
if fecha > hoy:
raise forms.ValidationError(
'La Feha no puede ser mayor al día de hoy')
class Meta:
model = Pagos
fields = ('carro', 'pago', 'fecha', 'semana', 'renta')
widgets = {'fecha': forms.DateInput(attrs={'type': 'date'}),
'semana': forms.DateInput(attrs={'type': 'week'})
}
views.py
class PagosCreate(CreateView):
form_class = PostPagos
template_name = "AC/add_expense.html"
def form_valid(self, form):
object = form.save(commit=False)
object.startweek, object.endweek = self.weekdatetimeconverter(
object.semana)
object.save()
return super(PagosCreate, self).form_valid(form)
def weekdatetimeconverter(self, semana):
d = semana
startweek = datetime.datetime.strptime(d + '-1', "%Y-W%W-%w")
endweek = datetime.datetime.strptime(d + '-0', "%Y-W%W-%w")
return (startweek, endweek)
models.py
class Pagos(models.Model):
carro = models.ForeignKey(
Carros, on_delete=models.CASCADE, blank=False, null=False)
pago = models.DecimalField(max_digits=6, decimal_places=2)
fecha = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
semana = models.CharField(max_length=20)
startweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
endweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
renta = models.ForeignKey(
Renta, on_delete=models.PROTECT, blank=False, null=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = "Pagos"
def get_absolute_url(self):
return reverse('pagos')
HTML
{% extends "base.html" %}
{% load widget_tweaks %}
{% block content %}
<div class='container'>
<form method="POST">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<div class="form-group">
<div class='column'>
<label>{{ field.label_tag }}</label>
{% render_field field class="form-control" %}
{% for error in field.errors %}
<span class="help-block">{{ error }}</span>
{% endfor %}
</div>
</div>
{% endfor %}
<div class="form-group">
<button type="submit" class="btn btn-success">
<span class="glyphicon glyphicon-ok"></span> Save
</button>
<a href="{% url 'pagos' %}" class="btn btn-default">Cancel</a>
</div>
</form>
</div>
{% endblock content%}
I'm using django-widget-tweaks
After reading again the documentation [https://docs.djangoproject.com/en/3.0/ref/forms/validation/#cleaning-a-specific-field-attribute][1]
one of my mistakes was that I was not doing a return, and it turns out the code needed was way simple to what I had, here it goes:
forms.py
class PostPagos(forms.ModelForm):
def clean_fecha(self):
fecha = self.cleaned_data['fecha']
if fecha > datetime.date.today():
raise forms.ValidationError(
'La Fecha no puede ser mayor al día de hoy')
return fecha
class Meta:
model = Pagos
fields = ('carro', 'pago', 'fecha', 'semana', 'renta')
widgets = {'fecha': forms.DateInput(attrs={'type': 'date'}),
'semana': forms.DateInput(attrs={'type': 'week'})
}