Thank you very much, Daniel!!! Max.
On Apr 30, 8:41 pm, Daniel Roseman <[email protected]> wrote: > On Apr 30, 5:43 pm, Lacrima <[email protected]> wrote: > > > > > Hello! > > > For example I have: > > class ContactForm(forms.Form): > > def __init__(self, foo, *args, **kwargs): > > super(ContactForm, self).__init__(*args, **kwargs) > > self.fields['subject'] = forms.CharField() > > self.fields['message'] = forms.CharField() > > self.fields['sender'] = forms.EmailField() > > if foo == 'boo': > > self.fields['cc_myself'] = forms.BooleanField() > > > In a view I have standard Django form processing like this: > > def contact(request): > > if request.method == 'POST': > > form = ContactForm(request.POST) > > if form.is_valid(): > > #processing the form here > > else: > > form = ContactForm('boo') > > return render_to_response('base_contact.html', { > > 'form': form, > > }) > > > But It seems that binding and validation doesn't work if I have custom > > __init__ method with an additional parameter. > > The form never is bound. > > After submitting the form I try the next in my debug probe:>>> print > > request.method > > POST > > >>> print request.POST > > > <QueryDict: {u'cc_myself': [u'on'], u'message': [u'I am here'], > > u'sender': [[email protected]'], u'subject': [u'Hello']}> > > > >>> form = ContactForm(request.POST) #I am trying to bind data to form > > >>> print form.is_bound #but it doesn't work > > False > > >>> print form.is_valid() #so form isn't valid > > False > > > So my question: what I am doing wrong when trying to override __init__ > > method? > > > And one more... If I delete additional parameter in __init__ > > definition then everything is ok: > > class ContactForm(forms.Form): > > def __init__(self, *args, **kwargs): > > super(ContactForm, self).__init__(*args, **kwargs) > > self.fields['subject'] = forms.CharField() > > self.fields['message'] = forms.CharField() > > self.fields['sender'] = forms.EmailField() > > self.fields['cc_myself'] = forms.BooleanField() > > > >>> print request.method > > POST > > >>> print request.POST > > > <QueryDict: {u'cc_myself': [u'on'], u'message': [u'I am here again'], > > u'sender': [[email protected]'], u'subject': [u'Hi again']}>>>> form = > > ContactForm(request.POST) > > >>> print form.is_bound > > True > > >>> print form.is_valid() > > > True > > > So, please, help me resolve this problem! > > > With regards, > > Max. > > > (sorry if my English isn't very proper) > > This is because your 'foo' variable is stealing the value of > request.POST you're passing into the form instantiation. > > You could do this: > form = ContactForm(foo, data=request.POST) > because you'll need to pass foo in. > > Or, define the _init__ like this: > def __init__(self, *args, **kwargs): > foo = kwargs.pop('foo', None) > ...etc... > if foo = 'boo' > -- > DR. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

