Django:基于类的视图无法呈现清晰的表格 [英] Django: Class based view can't render crispy form
问题描述
Stackoverflow大家好,
Hi Stackoverflow people,
我很难用基于类的视图来呈现酥脆的表格.当我使用基于函数的视图时,一切正常.
I have trouble to render a crispy form with a class based view. Everything worked fine when I used the function based views.
像往常一样,我按如下方式生成Forms.py:
As usual I generate forms.py as follows:
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Submit, HTML, Button, Row, Field
from item.models import Item
class CreateItemForm(forms.ModelForm):
class Meta:
model = Item
exclude = ('user',)
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.form_class = 'form-horizontal'
self.helper.layout = Layout(
Fieldset(
'Create your item here',
'name', 'description',
'save',
),
)
self.request = kwargs.pop('request', None)
return super(CreateItemForm, self).__init__(*args, **kwargs)
视图功能非常简单和标准:
The view function is very simple and standard:
from django.views.generic.edit import CreateView,
from item.models import Item
from item.forms import CreateItemForm
class ItemCreate(CreateView):
form_class = CreateItemForm
model = Item
template_name = 'item/item_create_form.html'
,模板也遵循以下最低要求:
and the template follows the minimal instructions as well:
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<h2>Create new Item</h2>
<form action="." class="crispy form-horizontal" method="post">
{% crispy form form.helper %}
</form>
{% endblock %}
我的问题是Django会抱怨在/item/add/处存在VariantDoesNotExist,在...中无法找到键[helper]."
My problem is that Django will complain "VariableDoesNotExist at /item/add/, Failed lookup for key [helper] in ...".
脆性表格与基于类的视图兼容吗?我如何交出帮助者信息以正确创建表单?
感谢您的帮助和建议.
推荐答案
构造函数过于缩进,这样,它属于表单的 Meta
类,但应直接位于CreateItemForm
constructor in your form is too indented, this way it belongs to form's Meta
class, but it should be directly in CreateItemForm
这篇关于Django:基于类的视图无法呈现清晰的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!