无法将 Helper 传递给模板中的 django Crispy Formset

Cannot pass Helper to django Crispy Formset in template

我正在尝试在表单和表单集(Visit 和 VisitService)上设置脆皮表单。

我无法将助手附加到表单集,无论我如何操作。 我将助手作为属性添加到 Formset 并将助手添加到视图和上下文,但它一直给我以下错误:

VariableDoesNotExist - 键查找失败 [helper] (也试过[helper_attribute])

这是我正在使用的:

forms.py:

class VisitForm(forms.ModelForm):
    class Meta:
        model = models.Visit
        fields = [
           [...all visit fields go here...]
        ]
    def __init__(self, *args, **kwargs):
        super(VisitForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.layout = Layout(
 [...fields go here...]

            ),
            ButtonHolder(
                Submit('submit', 'Submit', css_class='button white btn-wide')
            )
        )
class VisitServiceForm(forms.ModelForm):
    class Meta:
        model = models.VisitService
        fields = [
            'service',
            'unit',
        ]

    def __init__(self, *args, **kwargs):
        super(VisitServiceForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Row(
                Div(Field('service'),css_class='col-sm-4'),
                Div(Field('unit'),css_class='col-sm-4'),
        )
VisitServiceFormSet = forms.modelformset_factory(
    models.VisitService,
    form=VisitServiceForm,
    extra=2,
)
VisitServiceInlineFormSet = forms.inlineformset_factory(
    models.Visit,
    models.VisitService,
    extra=5,
    fields=('service', 'unit'),
    formset=VisitServiceFormSet,
    min_num=1,
)

views.py:

def create_visit(request, patient_pk):
    patient = get_object_or_404(models.Patient, pk=patient_pk)
    form_class = forms.VisitForm
    form = form_class()
    visitservice_forms = forms.VisitServiceInlineFormSet(
        queryset=models.VisitService.objects.none()
    )
    helper = forms.VisitServiceForm()

    if request.method == 'POST':
        form = form_class(request.POST)
        visitservice_forms = forms.VisitServiceInlineFormSet(
            request.POST,
            queryset=models.VisitService.objects.none()
        )
        if form.is_valid() and visitservice_forms.is_valid():
            visit = form.save(commit=False)
            visit.patient = patient
            visit.save()
            visitservices = visitservice_forms.save(commit=False)
            for visitservice in visitservices:
                visitservice.visit = visit
                visitservice.save()
            messages.success(request, "Added visit")
            return HttpResponseRedirect(visit.get_absolute_url())

    return render(request, 'fpform/visit_form.html', {
            'patient': patient,
            'form': form,
            'formset': visitservice_forms,
            'helper': helper,
    })

模板:

<div class="container">
    <form method="POST" action="">
        {% crispy form %}
        {% crispy formset formset.form.helper_attribute %}
        </div>
    </form>

在我的模板中,我也分别使用了其中的每一个,但没有成功:

{% crispy formset formset.form.helper %}
{% crispy formset helper_attribute %}
{% crispy formset helper %}
{% crispy formset form.helper_attribute %}
{% crispy formset form.helper %}

我查看了脆皮文档,但在那里找不到答案。

为此花了一整天,但它没有任何进展。如果有更好的方法来实现这一点,也请随时告诉我。

要定义表单集的布局,您需要创建一个独立的 FormHelper class 并将其传递给表单。

http://django-crispy-forms.readthedocs.io/en/latest/crispy_tag_formsets.html

# forms.py
class VisitServiceFormHelper(FormHelper):
    def __init__(self, *args, **kwargs):
        super(VisitServiceFormHelper, self).__init__(*args, **kwargs)
        self.layout = Layout(
            Div(Field('service'),css_class='col-sm-4'),
            Div(Field('unit'),css_class='col-sm-4'),
        )

#views.py
formset = VisitServiceInlineFormSet(queryset=VisitService.objects.none())
helper = VisitServiceFormHelper()
return render(request, 'template.html', {'formset': formset, 'helper': helper})

#template
{% crispy formset helper %}