Я хочу проверить и отобразить сообщение об ошибке над полем в наборе форм, когда после отправки набора форм срабатывает определенное условие.
Ошибка проверки правильности срабатывает и препятствует отправке формы, но сообщение об ошибке не отображается над / под полем. Как я могу получить его для отображения? Спасибо.
views.py
def create_quote(request,project_id):
project = get_object_or_404(ProjectDetails, id=project_id)
if request.method == 'POST':
formset = QuoteFormSet (request.POST,request.FILES,instance=project)
if formset.is_valid():
formset.save()
return redirect("viewBudgets",project_id=project.id)
else:
formset = QuoteFormSet (instance=project)
formset = QuoteFormSet (instance=project)
context = {
"project": project,
"formset": formset}
template = "projects/create_quote.html"
return render(request,template,context)
forms.py
class QuoteForm(ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.label_suffix = ""
def clean(self):
cleaned_data = super(QuoteForm, self).clean()
cost_desc = cleaned_data.get("cost_description")
total_amount = cleaned_data.get("total")
#Condition
if cost_desc == None and total_amount > 0:
raise forms.ValidationError("Cost Description is a required field.")
return cleaned_data
class Meta:
model = FinanceBudget
QuoteFormSet = inlineformset_factory(Projects,FinanceBudget,fields="__all__",form=QuoteForm,extra=10)
цитата. html
<table class="table table-light">
<tbody>
<form method="POST" autocomplete="off">
{{formset.management_form}}
{%csrf_token%}
{{ formset.non_form_errors.as_ul }}
<table id="formset" class="form">
{% for form in formset.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle row1 row2 %}">
{% for field in form.visible_fields %}
<td>
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>