简体   繁体   English

显示三个模型表单并使用django中的一个按钮提交

[英]Display three Model Forms and submit with single button in django

I have a complex and common required scenario in case of web pages/web development. 对于网页/ Web开发,我有一个复杂且常见的场景。

So i have three models like below 所以我有以下三种模式

models.py models.py

class model_a(models.Model):
    name_a = models.CharField(max_length=256)

class model_b(models.Model):
    forign_key_to_a = models.ForeignKey(model_a)
    name_b = models.CharField(max_length=256) 

class model_c(models.Model):
    forign_key_to_b = models.ForeignKey(model_b)
    name_c = models.CharField(max_length=256)

forms.py 表格

class Aform(ModelForm):
    model = model_a

class Bform(ModelForm):
    model = model_b

class Cform(ModelForm):
    model = model_c

So i have above models and forms , now i need to display all of them in a single page like 所以我有上述模型表格 ,现在我需要在一个页面中显示所有模型表格 ,例如

First display Aform 
Second display Bform 
      Here the user should be able to create multiple Bform's records for Aform
Third display Cform 
      here the user should be able to create multiple Cform's records for Bform

I am sorry if my explanation is bad, finally what all my intention is to add the functionality like model_a can have multiple model_b records, and model_b can have multiple model_c records something like below(I can't show u the design but i hope i explained the required functionality) 很抱歉,如果我的解释不好,最后我的全部意图是添加诸如model_a的功能可以具有多个model_b记录,而model_b可以具有多个model_c记录如下(我无法向您展示设计,但我希望我说明了所需的功能)

name_a : name_a:

   name_b(first record):
         name_c(first record):
         name_c(second record):
         name_c(third record):
   name_b(second record):     
         name_c(first record):
         name_c(second record):
         name_c(third record):
   name_b(third record):     
         name_c(first record):
         name_c(second record):
         name_c(third record):

So can anyone please let me how to add multiple records on a single page and submit with single button 任何人都可以让我如何在一个页面上添加多个记录并通过一个按钮提交

For adding the multiple records to a model by creating the forms dynamically i have tried using Django Formsets and jquery and i was facing this error here (of course this is another question but want to let u know what i have done in case of adding ,multiple records for a single model) 通过创建形式动态我已经尝试使用添加多个记录到一个模型Django Formsets和jQuery和我面临这个错误在这里 (当然这是另外一个问题,但要让你知道我在加入的情况下这样做,单个模型有多个记录)

You can add multiple forms to the context and access them in the template as follows: 您可以将多个表单添加到上下文中,并在模板中访问它们,如下所示:

<form action="" method="post">
    {% csrf_token %}
    {{ a_form.as_p }}
    {{ b_form.as_p }}
    {{ c_form.as_p }}
    <input type="submit" value="Submit" />
</form>

Then in the view you'd pull them out as follows: 然后在视图中按如下所示将它们拉出:

a_form = Aform(request.POST)
b_form = Bform(request.POST)
c_form = Cform(request.POST)

Edit: I just re-read your question and saw the part about needing multiple b_form's and c_form's on the page. 编辑:我只是重新阅读了您的问题,并在页面上看到了有关需要多个b_form和c_form的部分。 You could do that with some jQuery to copy the empty when it's needed. 您可以使用一些jQuery在需要时复制空值。

<button class="b_form_trigger">Add B Form</button>
<div class="hidden_b_form">{{b_form.as_p}}</div>
var b_form_counter = 0;
$('.b_form_trigger').click(function(){
    $(TARGET_ELEMENT).append($('.hidden_b_form > *').clone());
    // Then you will need to rename all of the inputs
    $(TARGET_ELEMENT).find('input').each(function(){
        // Need to add a prefix to the name field so we know what form we have.
        $(this).attr('name', 'b-form-'+b_form_counter+$(this).attr('name'));
    });
});

Then in your views.py file you can access each of the forms like this: 然后,在您的views.py文件中,您可以像下面这样访问每种形式:

counter = 0
b_forms = []
while 'b-form-%sname_c' % counter in request.POST:
    b_forms.append(Bform(request.POST, prefix="b-form-%s" % counter))
    counter += 1

You'll likely want to do something a bit more robust to support adding and removing of the forms, but this how I've been able to do this in the past. 您可能需要做一些更强大的工作来支持表单的添加和删除,但这是我过去能够做到的方式。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM