gpt4 book ai didi

Django CrispyForms - 在单独的引导选项卡中渲染表单集的每种形式

转载 作者:行者123 更新时间:2023-12-02 07:22:30 24 4
gpt4 key购买 nike

我想使用 django-crispyForms 在不同的 bootstrap3 选项卡中渲染表单集的每种形式,但这似乎并不简单,因为 CrispyForms 尚未完全处理表单集。

enter image description here

我的表格:

class BlogMessageForm(forms.ModelForm):
class Meta:
model = BlogMessage
fields = ['field1', 'field2', ]

class BlogMessageFormsetHelper(FormHelper):
def __init__(self, *args, **kwargs):
super(BlogMessageFormsetHelper, self).__init__(*args, **kwargs)
self.form_tag = False
self.layout = Layout(
TabHolder(
Tab('Form%s' % (form.pk),
'field1', 'field2', 'DELETE',
)
)

)

我的观点:

def all_blogs_messages_in_bootstrap_tab(request):
all_blog_messages = BlogMessage.objects.all()
blogMessageForm = modelformset_factory(BlogMessage, form=BlogMessageForm, extra=1, can_delete=True)
formset = blogMessageForm(queryset=all_blog_messages)
helper = BlogMessageFormHelper()

render_to_response("blogs/blogMessage_forms.html", {'formset': formset, 'helper': helper}, context_instance=RequestContext(request))

我的 CrispyForms 模板:

{% load crispy_forms_tags %}
<form method="post" action="" encrypt="multipart/form-data">{% csrf_token %}
{% crispy formset helper %}
</form>

这里有一些关于 CripyForm 表单集约束的信息:

最佳答案

这是我的做法:

<form method="post" enctype="multipart/form-data">{% csrf_token %}
{{ formset.management_form }}
<div role="tabpanel">
<ul class="nav nav-tabs" role="tablist">
{% for form in formset %}
<li role="presentation" class="{% if forloop.first %}active{% endif %}">
<a href="#id_form-{{ forloop.counter0 }}" aria-controls="id_form-{{ forloop.counter0 }}" role="tab" data-toggle="tab">
{% if forloop.counter0 < formset.initial_forms|length %}
Form{{ forloop.counter }}
{% else %}
New Form
{% endif %}
</a>
</li>
{% endfor %}
</ul>
<div class="tab-content">
{% for form in formset %}
<div role="tabpanel" class="tab-pane{% if forloop.first %} active{% endif %}" id="id_form-{{ forloop.counter0 }}">
{% crispy form %}
{% if form.instance.pk %}
<input id="id_form-{{ forloop.counter0 }}-id" name="form-{{ forloop.counter0 }}-id" type="hidden" value="{{ form.instance.id }}">
{% endif %}
{% if forloop.counter0 < formset.initial_forms|length %}
{% if formset.can_delete %}
<input id="id_form-{{ forloop.counter0 }}-DELETE" name="form-{{ forloop.counter0 }}-DELETE" type="hidden">
<button type="submit" data-id="id_form-{{ forloop.counter0 }}-DELETE" class="btn btn-default btn-formset-delete">Delete</button>
{% endif %}
{% endif %}
</div>
{% endfor %}
</div>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>

差点忘了,如果没有一些 JavaScript,删除按钮将无法工作。但这当然是可选的,可以使 delete 字段可见并删除按钮,否则:

$(document).ready(function(){
$(".btn-formset-delete").click(function(){
$("#" + $(this).data('id')).val("on");
return true;
});
});

还将其添加到表单中:

def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_tag = False
self.helper.disable_csrf = True

关于Django CrispyForms - 在单独的引导选项卡中渲染表单集的每种形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28919825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com