Re: Multiple instances of the same form on one page: how to get the right POST data?
On Wed, Aug 5, 2009 at 11:16 PM, Berco Beute wrote: > >> 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. > > Thanks, the missing prefix was indeed one issue, although > prepopulating with data is not a problem. It didn't solve the problem > though. The problem arises from the fact that I'm rendering multiple > forms, but the user is POSTing back only one form. Each form has it's > own submit field. How do I know which form was submitted? I've tried > stripping the prefix from the subitted form, but that felt kind of > hacky (and didn't really work). > how about hidden input? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Multiple instances of the same form on one page: how to get the right POST data?
> 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. Thanks, the missing prefix was indeed one issue, although prepopulating with data is not a problem. It didn't solve the problem though. The problem arises from the fact that I'm rendering multiple forms, but the user is POSTing back only one form. Each form has it's own submit field. How do I know which form was submitted? I've tried stripping the prefix from the subitted form, but that felt kind of hacky (and didn't really work). > 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). You are absolutely right, I added that. Thanks. 2B --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Multiple instances of the same form on one page: how to get the right POST data?
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 -~--~~~~--~~--~--~---
Multiple instances of the same form on one page: how to get the right POST data?
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)] return render_to_response('invite_resp.html', {'invite_forms': invite_forms}, context_instance=RequestContext(request)) =forms.py class InviteForm(forms.ModelForm): def __init__(self, label, *args, **kwargs): super(InviteForm, self).__init__(*args, **kwargs) self.fields['status'].label = label class Meta: model = Invite exclude = ('invitation', 'email_address') =page.html=== {% block content %} {% for invite_form in invite_forms %} {{ invite_form.status.label }} {{ invite_form.status }} {% endfor %} {% endblock %} --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---