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

时间:2017-03-05 23:34:13

标签: django django-templates django-views django-crispy-forms formset

我正在尝试在表单和formset(Visit和VisitService)上设置crispy表单。

无论我怎么做,我都无法将帮助程序附加到formset上。 我将帮助器作为属性添加到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 %}

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

整整一天花在这上面但它没有去过任何地方。如果有更好的方法可以让我知道,还可以随时告诉我。

1 个答案:

答案 0 :(得分:0)

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

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 %}