未显示 Django ValidationErrors
Django ValidationErrors not being displayed
我在用户选择未来日期时对日期选择器应用验证。到目前为止,表单没有保存无效条目,但问题是它没有显示该字段的错误消息。这是我到目前为止尝试过的:
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
再次阅读文档后[https://docs.djangoproject.com/en/3.0/ref/forms/validation/#cleaning-a-specific-field-attribute][1]
我的一个错误是我没有做 return,事实证明所需的代码比我拥有的代码简单得多,这里是:
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'})
}
我在用户选择未来日期时对日期选择器应用验证。到目前为止,表单没有保存无效条目,但问题是它没有显示该字段的错误消息。这是我到目前为止尝试过的:
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
再次阅读文档后[https://docs.djangoproject.com/en/3.0/ref/forms/validation/#cleaning-a-specific-field-attribute][1]
我的一个错误是我没有做 return,事实证明所需的代码比我拥有的代码简单得多,这里是:
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'})
}