开发者

Processing forms that generate many rows in DB

开发者 https://www.devze.com 2022-12-29 11:58 出处:网络
I\'m wondering what the best approach to take here is. I\'ve got a form that people use to register for a class and a lot of times the ma开发者_Python百科nager of a company will register multiple peop

I'm wondering what the best approach to take here is. I've got a form that people use to register for a class and a lot of times the ma开发者_Python百科nager of a company will register multiple people for the class at the same time. Presently, they'd have to go through the registration process multiple times and resubmit the form once for every person they want to register.

What I want to do is give the user a form that has a single <input/> for one person to register with, along with all the other fields they'll need to fill out (Email, phone number, etc); if they want to add more people, they'll be able to press a button and a new <input/> will be generated. This part I know how to do, but I'm including it to best describe what I'm aiming to do.

The part I don't know how to approach is processing that data the form submits, I need some way of making a new row in the Registrant table for every <input/> that's added and include the same contact information (phone, email, etc) as the first row with that row. For the record, I'm using the Django framework for my back-end code.

What's the best approach here? Should it just POST the form x times for x people, or is there a less "brute force" way of handling this?


Django includes FormSet for dealing with exactly these challenges. Using a FormSet you can create multiple forms for creating or updating information. There's even possible to generate the FormSets from a Model. http://docs.djangoproject.com/en/dev/topics/forms/formsets/ and http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#id1 are great resources.

Now, for creating more forms on the fly, you need some javascript magic. I've done this on work projects using jQuery which made it a lot simpler. The basic idea is create a new form with the correct inputs and change the hidden metadata in the formset form so it will now how many forms to process. The admin implements this when using multiple inline forms so I suggest looking there for code as it is a bit tricky to get right.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号