On Tue, 2009-08-04 at 07:07 -0700, Berco Beute wrote: > I'm trying to add multiple instances of the same form on one page, > each with its own submit button. I know how to use the 'prefix' > argument for a form to differentiate between forms, but I can't figure > out how to get the right data when it is POST'ed back (see view.py > below). Somehow the form stays invalid, no matter what I try. Any > suggestions what I may be doing wrong? > > Thanks! > > =models.py======================= > class Invite(models.Model): > invitation = models.ForeignKey(Invitation, verbose_name=_ > ('User')) > email_address = models.EmailField(_('email address')) > status = models.CharField(_('RSVP status'), max_length=1, > choices=settings.INVITE_RESPONSE_CHOICES) > ======================== > > > =views.py=================== > def invite_resp(request): > invites = Invite.objects.all() > if request.method == 'POST': > invite_formz = [InviteForm(request.POST, prefix=str(index), > instance=invite) for index, invite in enumerate(invites)] > if all([invite_form.is_valid() for invite_form in > invite_formz]): > invite = invite_form.save() > return HttpResponseRedirect('/') > invite_forms = [InviteForm(instance=invite) for index, invite in > enumerate(invites)]
You forgot to set the prefix here, so the form that the user is submitting their data from doesn't have the fields named correctly. You need to set it up exactly the same way at this point as you did in the POST path, with the exception of not prepopulating with data. Also, you've almost certainly left off an "else" clause here. You only want to create new, empty forms if the method is not POST (you can fall through to this point with request.method == "POST" and one of the forms being invalid). Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---