问题描述
我有一个django ,我想在中间进行布局另一种形式。我使用设置父窗体的 __ init __
: from crispy_forms.helper import FormHelper
from crispy_forms.layout import提交,布局,字段,
def __init __(self,* args,** kwargs):
self.helper = FormHelper()
self.helper.layout =布局(
Div(
Div(Field('foo'),css_class ='span3'),
Div(Field('bar'),css_class ='span4'),
css_class ='row'
),
Field('baz',css_class ='span1'),
...
)
self.helper.add_input(Submit('submit','Submit ',css_class = btn btn-primary offset4'))
我的模板只是使用 {%crispy%}
标签。
我想知道我应该如何整合表单。我应该在上面的init函数中实例化吗?我该怎么参考呢?
有其他例子,其中有一个呈现在另一个连续的形式和formset组合,但我想知道我是否可以更好地控制它们如何与松脆的布局配合。 > 谢谢! 我解决了这个问题,而不修改Crispy Forms,新的字段类型呈现一个表单集: 它需要一个模板来呈现表单集,这样可以控制它的呈现方式: 您可以使用它在布局中嵌入一个formset,就像任何其他Crispy布局元素一样: p> I have a django Formset that I'd like to layout in the middle of another form. I'm using django-crispy-forms to set the layout in the parent form's My template simply renders the form using the I'd like to know how I should incorporate the formset. Should I instantiate it in the above init function? How do I refer to it there? There are other examples of form and formset combos online that have one render after the other serially, but I'm wondering whether I can have more control over how they fit together with crispy's layout. Thanks! I solved this without modifying Crispy Forms, by creating a new field type that renders a formset: It needs a template to render the formset, which gives you control over exactly how it's rendered: You can use it to embed a formset in your layouts just like any other Crispy layout element: 这篇关于django脆皮形式:将表单嵌套在表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
from crispy_forms.layout import LayoutObject,TEMPLATE_PACK
pre>
class Formset (LayoutObject):
布局对象它呈现整个表单,就像它是一个字段
示例::
Formset(attached_files_formset)
template =%s / formset.html%TEMPLATE_PACK
def __init __(self,formset_name_in_context,template =无):
self.formset_name_in_cont ext = formset_name_in_context
#crispy_forms / layout.py:302要求我们有一个字段属性
self.fields = []
#覆盖类变量实例级变量
如果模板:
self.template = template
def render(self,form,form_style,context,template_pack = TEMPLATE_PACK):
formset = context [self.formset_name_in_context]
return render_to_string(self.template,Context({'wrapper':self,
'formset':formset}))
{%load crispy_forms_tags%}
< div class =formset>
{%crispy formset%}
< input type =buttonname =addvalue =添加另一个/>
< / div>
self.helper.layout = Layout(
MultiField(
Education,
Formset '教育'),
),
__init__
:from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Field, Div
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.layout = Layout(
Div(
Div(Field('foo'), css_class='span3'),
Div(Field('bar'), css_class='span4'),
css_class='row'
),
Field('baz', css_class='span1'),
...
)
self.helper.add_input(Submit('submit', 'Submit', css_class='btn btn-primary offset4'))
{% crispy %}
tag.from crispy_forms.layout import LayoutObject, TEMPLATE_PACK
class Formset(LayoutObject):
"""
Layout object. It renders an entire formset, as though it were a Field.
Example::
Formset("attached_files_formset")
"""
template = "%s/formset.html" % TEMPLATE_PACK
def __init__(self, formset_name_in_context, template=None):
self.formset_name_in_context = formset_name_in_context
# crispy_forms/layout.py:302 requires us to have a fields property
self.fields = []
# Overrides class variable with an instance level variable
if template:
self.template = template
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK):
formset = context[self.formset_name_in_context]
return render_to_string(self.template, Context({'wrapper': self,
'formset': formset}))
{% load crispy_forms_tags %}
<div class="formset">
{% crispy formset %}
<input type="button" name="add" value="Add another" />
</div>
self.helper.layout = Layout(
MultiField(
"Education",
Formset('education'),
),