django - inlineformset - crispy - 如何显示non_field_errors

时间:2018-03-16 15:55:07

标签: python django django-crispy-forms inline-formset

我有以下CreateView

class CreatePerson(CreateView):
    model=Person
    form_class=PersonForm

    def get_context_data(self, **kwargs):

        context = super(CreatePerson, self).get_context_data(**kwargs)
        if self.request.POST:
            context['address_formset'] = AddressInlineFormSet(self.request.POST)
        else:
            context['address_formset'] = AddressInlineFormSet()
        return context

    def form_valid(self, form):
        context = self.get_context_data()
        formset = context['address_formset']
        if formset.is_valid():
            self.object = form.save()
            formset.instance = self.object
            formset.save()
            return super().form_valid(form)
        else:
            return self.render_to_response(self.get_context_data(form=form))

以及以下表格

{% load static crispy_forms_tags %}
...
    {% csrf_token %}
    {% crispy form %}

    {{ address_formset.management_form }}

    {% for formset in address_formset %}
        {% crispy formset %}
    {% endfor %}
    ...

如何在表单中显示重复键错误(在formset模型中出现)?

我试过这种方式。

form_errors = formset.errors
return self.render_to_response(self.get_context_data(form=form,form_errors=form_errors))

{{ form_errors }}

,表格中显示以下内容。

[{}, {'__all__': ['Please correct the duplicate values below.']}]

如何以清脆的方式呈现它?

由于

1 个答案:

答案 0 :(得分:0)

我需要的只是......

{% csrf_token %}

{% crispy form  %}

{% crispy address_formset address_formset.form.helper %}