简体   繁体   English

我可以在django中以单一形式使用多个formset,如果是的话怎么样?

[英]Can I use multiple number of formset in a single form in django,if yes how?

I have to make a form in which more than one formset is used. 我必须制作一个使用多个formset的表单。 please tell me if this is possible. 请告诉我这是否可行。 if yes then how? 如果是的话怎么样?

You can add as many formsets in the form. 您可以在表单中添加任意数量的表单集。 Just create/init them in view and pass to template to render in the form. 只需在视图中创建/初始化它们并传递给模板以在表单中呈现。

Something like: 就像是:

{{ formset1.management_form }}
{% for form in formset1 %}
    {{ form }}
{% endfor %}

{{ formset2.management_form }}
{% for form in formset2 %}
    {{ form }}
{% endfor %}

You are using multiple formsets in one view, you need to use prefix for the forms as explained here Using more than one formset in a view In short: 您在一个视图中使用多个formset,您需要为表单使用前缀,如此处所述在视图中使用多个formset简而言之:

article_formset = ArticleFormSet(prefix='articles')
book_formset = BookFormSet(prefix='books')

When adding a prefix='article' We need to consider that the id of the formset will change and there are other changes too like 添加前缀='文章'时我们需要考虑到formset的id会发生变化,还有其他变化

<label for="id_form-0-title">Title:</label> 

<label for="id_article-0-title">Title:</label>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Django中将输入HTML与formset一起使用 - How can I use input HTML with formset in Django 如何在HTML和Django中以单一形式使用2个提交按钮? - How can I use 2 submit buttons in a single form in HTML & Django? 如何让表单集继承表单的样式? 我的意思是,如何在 Django 中设置表单集的样式? - How do I make the formset inherit the styles of the form? I mean, how do I style the formset in Django? 在Django 1.11中,如何使用formset_factory呈现表单 - In django 1.11, how can I render form using formset_factory 表单(或Formset?)来处理Django中的多个表行 - Form (or Formset?) to handle multiple table rows in Django 如何使用 django formset_factories? - How do I use django formset_factories? 如何使用 Django Rest Framework 在单个请求中反序列化多个文件和表单数据? - How can I deserialize multiple files and form data in a single request using Django Rest Framework? 我可以使用 android studio 代替 Pycharm 来学习 Django .. 如果没有.. 为什么? 如果是..如何? - Can I use android studio in place of Pycharm for learning Django .. If No.. why? and if Yes ..how? Django表单集和表单ValidationError - Django formset and form ValidationError 将django表单(formset)中的十进制数加载到数字输入中 - Loading decimal numbers from django form (formset) into number input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM