On Aug 5, 7:16 am, Ed Schofield <edschofi...@gmail.com> wrote: > On Aug 5, 2:23 pm, Karen Tracey <kmtra...@gmail.com> wrote: > > > > > > > On Thu, Aug 5, 2010 at 12:10 AM, Ed Schofield <edschofi...@gmail.com> wrote: > > > Hi everyone, > > > > I'm trying to use a view with multiple forms under Django 1.2.1. I'm > > > puzzled that the prefix parameter seems to screw up validation. Here's > > > a test case: > > > > >>> from django import forms > > > > >>> class MyForm(forms.Form): > > > >>> field1 = forms.IntegerField(required=False) > > > >>> field2 = forms.IntegerField() > > > > >>> f1 = MyForm(data={'field1': 1, 'field2': 2}) > > > >>> f2 = MyForm(data={'field1': 1, 'field2': 2}, prefix='p') > > > > >>> f1.is_valid() > > > True > > > > >>> f2.is_valid() > > > False > > > > Can anyone explain why these are different? > > > You need to include the prefix in the data dictionary keys for the form. > > See:http://code.djangoproject.com/ticket/13763#comment:3 > > I've done some more digging. It seems that the prefix on data > dictionary keys is required only when initializing the form, not when > accessing the form data: > > >>> f1.cleaned_data > > {'field1': 1, 'field2': 2} > > >>> f2.cleaned_data > > {'field1': 1, 'field2': 2} > > rather than what I would now expect, given how the data argument is > processed: > > {'p-field1': 1, 'p-field2': 2} > > Isn't this oddly inconsistent? > > -- Ed
Not at all. The prefix is added to the form fields when they are displayed, so that form 1's field1 and form2's field1 get different HTML names, and therefore different keys in the POST data dictionary. However the developer still wants to access form1.field1 and form2.field1 as they were originally defined - if they wanted form1.p- field1, they should have defined it that way in the first place. -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.