简体   繁体   English

Django - 在视图中提交多个表单

[英]Django - submit multiple forms in a view

I have two forms in my views, when I hit on save it's not working properly, when I want to display on my templates what I saved not showing as expected.我的视图中有两个表单,当我点击保存时它无法正常工作,当我想在我的模板上显示我保存的内容时没有按预期显示。

Here's what I have:这是我所拥有的:

views.py视图.py

def index(request):
    queryset = Personinfo.objects.all()
    queryset2 = Person.objects.all()
    qs = chain(queryset,queryset2)
    form = personform(request.POST or None)
    form2 = personinfoform(request.POST or None)
    context = {
    "queryset": queryset,
    "queryset2": queryset2,
    "qs": qs,
    "form2":form2,
    "form":form,
    }
    form2_valid = form2.is_valid()
    form_valid = form.is_valid()
    if form2_valid and form_valid:
        a = form2.save()
        b= form.save(commit=False)
        b.ForeignkeytoA = a
        b.save()
    return render(request, "index.html", context)

index.html索引.html

<form method="POST" action="">{% csrf_token %}
{{form2.as_p}}
{{form.as_p}}
<input type="submit" value="Save!" />
</form>

            <table >
             <tr>
            <th>Name</th>
            <th>Address</th>
            <th>Number</th>
            <th>Hobbies</th>

            </tr>
            {% for item in qs  %}
            <tr> 
                <td>{{ item.name }}</td> #form2
                <td>{{ item.address }}</td> #form1
                <td>{{ item.phone_number }}</td> #form1
                <td>{{ item.address }}</td> #form1
            </tr>
            {% endfor %}
            </table>

models.py模型.py

class Personinfo(models.Model):
    name = models.CharField(max_length=128)

    def __str__(self):
        return self.name





class Person(models.Model):
    person = models.ForeignKey(Personinfo)
    address = models.TextField()
    phone_number = models.CharField(max_length=128)
    hobbies =models.CharField(max_length=128)

    def __str__(self):
        return self.person

my output:我的输出:

点击这里

As you can see my table isn't showing my items as expected.如您所见,我的表格没有按预期显示我的项目。 Is there a possible way to show every item in the same row?有没有办法在同一行中显示每个项目?

Two errors are present.存在两个错误。 If I understand right, you're expecting the data from a Person instance and the data from its accompanying PersonInfo instance to print on the same line.如果我理解正确,您希望来自Person实例的数据和来自其随附的PersonInfo实例的数据打印在同一行上。 However, you're trying to achieve this by using chain , which is not joining the querysets based on their relationship, but rather concatenating them blindly.但是,您试图通过使用chain来实现这一点,这不是根据它们的关系加入查询集,而是盲目地连接它们。

So if Person.objects.all() returns a queryset which contains the following data因此,如果Person.objects.all()返回一个包含以下数据的查询集

id  person   address   phone_number    hobbies   
1      1         a         a             a
2      2         5         5             5

and PersonInfo.objects.all() returns a queryset which containsPersonInfo.objects.all()返回一个查询集,其中包含

id   Name 
1    aaa      
2    aa       

chain combines them as chain将它们组合为

id  person  name   address   phone_number    hobbies
1           aaa
2           aa
1      1             a           a             a
2      2             5           5             5

Instead, you should utilize the relationship between the models.相反,您应该利用模型之间的关系。 If you pass only the Person queryset as context to your template, you could write如果您仅将Person作为上下文传递给模板,则可以编写

{% for p in persons %}
    <tr>
        <td>{{ p.person.name }}</td>
        <td>{{ p.address }}</td>
        <td>{{ p.phone_number }}</td>
        <td>{{ p.hobbies }}</td>
    </tr>
{% endfor %}

-- ——

Additionally you are setting the Personinfo related instance incorrectly when you save your forms.此外,您在保存表单时错误地设置了与Personinfo相关的实例。 By using b.ForeignkeytoA you are creating a new variable as a member of the object b called ForeignkeytoA , which has nothing to do with the Personinfo relationship.通过使用b.ForeignkeytoA您正在创建一个新变量作为对象b的成员,称为ForeignkeytoA ,它与Personinfo关系无关。 To set the related Personinfo , you should reference the name of the foreign key field, person .要设置相关的Personinfo ,您应该引用外键字段的名称person To correct this, that segment should be要纠正这一点,该段应该是

# ...
b = form.save(commit = False)
b.person = a
b.save()
# ...

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

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