Django:如何在Django表单中多次包含相同的字段? [英] Django: how do I include the same field multiple times in a Django form?
问题描述
我的Django应用程序中有一个基本的朋友邀请表,如下所示:
I've got a basic friend invite form in my Django app that looks like this:
class FriendInviteForm(forms.Form):
email = forms.EmailField()
当用户是邀请一个人,但我想允许用户邀请他们想要的许多朋友。我试图构建的实现是使用3个电子邮件字段显示表单,并允许用户使用jQuery在客户端上动态添加更多字段。
It works perfectly when a user is inviting a single person, but I'd like to allow the user to invite as many of their friends as they'd like. The implementation I am trying to build would be to display the form with 3 email fields, and allow the user to dynamically add more fields on the client side using jQuery.
处理这样一个表单的创建方法是什么?我将如何处理我的视图中的电子邮件字段?我可以继续使用Django的内置表单吗?
What is the best way to handle the creation of such a form, and how would I process the email fields in my view? Can I still use Django's built in forms for this?
谢谢!
推荐答案
我认为,formets在这里没有帮助,因为OP希望有一个单一的形式。我只需要一个带有多行文本字段的表单,要求用户每行输入一封电子邮件,或者以分号分隔,并在提交表单时解析邮件。
I think formsets does not help here much because the OP wants to have a single form. I would just make a form with a multiline text field, ask the user to enter one email per line, or separate them by semicolons, and parse emails from there when the form is submitted.
如果您确实需要单独的字段,我将查看数组字段。我自己也没有这样做,但例如,这里有一个类似的问题: Django等价于PHP的表单值数组/关联数组
If you do need to have separate fields I would look into array fields. I myself have not done this, but for example, here is a similar question: Django equivalent of PHP's form value array/associative array
或者也许这一个更好: Django:使用django格式创建HTML输入数组
or maybe this one is even better: Django: create HTML input array using a django form
这篇关于Django:如何在Django表单中多次包含相同的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!