I’ve got a basic friend invite form in my Django app that looks like this:
class FriendInviteForm(forms.Form):
email = forms.EmailField()
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.
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?
Thanks!
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.
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
or maybe this one is even better: Django: create HTML input array using a django form