在 crispy formset 上查找 u' 中的键 [form] 失败

Failed lookup for key [form] in u' on crispy formset

当我点击提交按钮时,我不断收到 Failed lookup for key [form] in u'(...)。我不知道发生了什么或为什么。不知道是酥脆的,还是造型的问题,还是观点的问题。也许一个提示是我在 formset 中放置了 extra=1 但只显示了 1 行。

form.py

class CotizacionForm(forms.ModelForm):
    class Meta:
        model = Cotizacion
        fields = ('fecha', 'local','estado_pago','estado_trabajo','detalle_monto', 'observacion',)
        widgets = {'fecha': forms.DateInput(attrs={'id': 'datetimepicker12'})}



    def __init__(self, *args, **kwargs):
        super(CotizacionForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id = 'id-cotizacion-form'
        self.helper.form_method = 'post'
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-3 text-left'
        self.helper.field_class = 'col-lg-9'
        self.helper.layout = Layout(
                Div(
                    Field('fecha',style="margin-bottom: -10px;"),
                    Field('local',style="margin-bottom: -10px;"),
                    Field('estado_trabajo', style="margin-bottom: -10px;"),
                    Field('estado_pago', style="margin-bottom: -10px;"),
                    Field('detalle_monto', style="margin-bottom: -10px;"),
                    css_class="col-lg-6"
                    ),
                Div(

                    Field('observacion',style="max-height: 100px;"),
                    css_class="col-lg-6"
                    ),
                )



class TrabajoForm(forms.ModelForm):
    class Meta:
        model = Trabajo

        fields = ('unidad','valor','actividad','cuadrilla')

    descripcion = forms.CharField(
            label = "Escriba el detalle del trabajo",
            max_length = 200,
            required = True,
            )

    def __init__(self, *args, **kwargs):
        super(TrabajoForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id = 'id-trabajo-form'
        self.helper.form_method = 'post'
        # self.helper.form_action = reverse('submit_form')
        # self.helper.add_input(Submit('submit', '+', css_class='btn btn-rounded btn-sm btn-icon btn-default text-right'))
        self.helper.form_class = 'form-inline col-lg-12'
        self.helper.field_template = 'bootstrap3/layout/inline_field.html'
        self.helper.layout = Layout(
                Div(
                    InlineField('descripcion', 
                        style="width:650px"
                        ),
                    'unidad',
                    'valor',
                    'actividad',
                    'cuadrilla',
                    HTML("<a class=" + "delete-row" + " href=" + "javascript:void(0)" + "><i class=" + "'fa fa-trash-o fa-lg icon text-danger-dker'" + "></i>  </a>"),
                    style='margin-bottom:5px',
                    # css_class="col-lg-12"
                    ),
                ButtonHolder(
                Submit('submit', 'Submit', css_class='button white')
            )
                    )

views.py

@login_required
def cotizacion_new(request, cotizacion_id=None):
    TrabajoInlineFormSet = inlineformset_factory(Cotizacion, Trabajo, form=TrabajoForm, extra=1, can_delete=True)

    if cotizacion_id:
        cotizacion = Cotizacion.objects.get(pk=cotizacion_id)

    else:
        cotizacion = Cotizacion()

    if request.method == "POST":
        form = CotizacionForm(request.POST, instance=cotizacion)
        formset = TrabajoForm(request.POST, instance=cotizacion)
        if form.is_valid() and formset.is_valid():
            form.save()
            formset.save()
            return redirect('erp:cotizacion_detail', pk=cotizacion.pk)
    else:
        form = CotizacionForm(instance=cotizacion)
        formset = TrabajoInlineFormSet(instance=cotizacion)

    return render(request, 'erp/cotizacion_edit.html', {'form': form, 'formset': formset})

cotizacion_edit.html

                <div class="col-lg-12">
                    <div class="row">
                        {% crispy form %}
                    </div>
                    <hr>
                    <p>Detalle de los Trabajos:</p>
                    <div class="row link-formset">
                        {% crispy formset formset.form.helper %}
                    </div>
                </div>
                <form method='POST' action=''>
                {% csrf_token %}
                <div class="col-lg-12 ">
                    <br>
                    <input type="submit"  class="btn btn-sm btn-block btn-primary"></input> 
                    <br>
                </div>
                </form>
            </div>

urls.py

(...)
url(r'^cotizacion/new/$', views.cotizacion_new, name='cotizacion_new'),
    url(r'^cotizacion/edit/(?P<cotizacion_id>[0-9]+)/$', views.cotizacion_new, name='cotizacion_edit'),
    url(r'^cotizacion/(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='cotizacion_detail'),

环境:

请求方法:POST 请求 URL:http://localhost:8000/erp/cotizacion/new/

Django 版本:1.11.4 Python版本:2.7.10

Installed Applications:
['erp.apps.ErpConfig',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'crispy_forms']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

模板错误:

回溯:

内部文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/core/handlers/exception.py” 41.响应=get_response(请求)

_legacy_get_response 中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/core/handlers/base.py” 249.响应=self._get_response(请求)

文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/core/handlers/base.py”在_get_response 187.响应=self.process_exception_by_middleware(e,请求)

文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/core/handlers/base.py”在_get_response 185.响应=wrapped_callback(请求,*callback_args,**callback_kwargs)

_wrapped_view 中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/contrib/auth/decorators.py” 23. return view_func(请求,*args,**kwargs)

cotizacion_new 中的文件“/Applications/MAMP/htdocs/italuzbi/erp/views.py” 76. return 渲染(请求,'erp/cotizacion_edit.html',{'form':表单,'formset':表单集})

渲染中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/shortcuts.py” 30. content = loader.render_to_string(template_name, context, request, using=using)

render_to_string 中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/loader.py” 68. return template.render(上下文,请求)

渲染中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/backends/django.py” 66.return self.template.render(上下文)

渲染中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/base.py” 207.returnself._render(上下文)

_render 中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/base.py” 199.returnself.nodelist.render(上下文)

渲染中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/base.py” 990. 位 = node.render_annotated(上下文)

render_annotated 中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/base.py” 957.returnself.render(上下文)

渲染中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/loader_tags.py” 177.returncompiled_parent._render(上下文)

_render 中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/base.py” 199.returnself.nodelist.render(上下文)

渲染中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/base.py” 990. 位 = node.render_annotated(上下文)

render_annotated 中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/base.py” 957.returnself.render(上下文)

渲染中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/loader_tags.py” 72.结果=block.nodelist.render(上下文)

渲染中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/base.py” 990. 位 = node.render_annotated(上下文)

render_annotated 中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/base.py” 957.returnself.render(上下文)

渲染中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/crispy_forms/templatetags/crispy_forms_tags.py” 215.c = self.get_render(上下文)

get_render 中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/crispy_forms/templatetags/crispy_forms_tags.py” 109. helper = helper.resolve(上下文)

解析文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/base.py” 849.值=self._resolve_lookup(上下文)

_resolve_lookup 中的文件“/Applications/MAMP/htdocs/italuzbi/italuzvenv/lib/python2.7/site-packages/django/template/base.py” 903. (bit, current)) # 缺少属性

我认为问题出在您调用 {% crispy formset.form.helper %} 的地方 - 表单集没有 form 属性。请参阅 the crispy docs 了解如何将该标签与表单集一起使用。

如果要在同一个模板中处理 2 个或更多表单并将提交按钮放在表单外,则必须删除表单标签:

self.helper.form_tag = False

文档在 http://django-crispy-forms.readthedocs.io/en/d-0/tags.html