Re: problem with conditional form
Thanks. I'll give that a shot... On Feb 24, 12:17 pm, Michael Elkinswrote: > On Fri, Feb 24, 2012 at 10:21:23AM -0800, Len De Groot wrote: > >I added a model to set the form-type to "0" or "1" which corresponds > >to "Long form" and "Short form" > > >When I create a new long form and add the following to the form page > >template I get the expected answer: > > >{{app.workshop.type.get_form_type_display}} results in "Long form" > > >I'm trying to use this syntax to write an if statement that shows or > >hides an element based on the form_type. So when I'm in my forms.py > >doc I include this: > > > if self.workshop.type.form_type == '0': > > home_address = forms.CharField( > > label="Home Street Address", > > required=False, > > ) > > >Self returns the error "not defined". As does instance, this, etc. > > Without seeing the surrounding context, it is difficult to say > what is wrong with the code. > > However, I think that an alternative approach might be to declare > a form class for your short form, and then have the long form > inherit from the short form and add the extra fields you want: > > class ShortForm(forms.Form): > name = forms.CharField() > > class LongForm(ShortForm): > home_address = forms.CharField( > label="Home Street Address", > required=False, > ) > > In your view, you just create a ShortForm() or LongForm() as > needed. Your template won't need to be aware of the difference. -- 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: problem with conditional form
On Fri, Feb 24, 2012 at 10:21:23AM -0800, Len De Groot wrote: I added a model to set the form-type to "0" or "1" which corresponds to "Long form" and "Short form" When I create a new long form and add the following to the form page template I get the expected answer: {{app.workshop.type.get_form_type_display}} results in "Long form" I'm trying to use this syntax to write an if statement that shows or hides an element based on the form_type. So when I'm in my forms.py doc I include this: if self.workshop.type.form_type == '0': home_address = forms.CharField( label="Home Street Address", required=False, ) Self returns the error "not defined". As does instance, this, etc. Without seeing the surrounding context, it is difficult to say what is wrong with the code. However, I think that an alternative approach might be to declare a form class for your short form, and then have the long form inherit from the short form and add the extra fields you want: class ShortForm(forms.Form): name = forms.CharField() class LongForm(ShortForm): home_address = forms.CharField( label="Home Street Address", required=False, ) In your view, you just create a ShortForm() or LongForm() as needed. Your template won't need to be aware of the difference. -- 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.
problem with conditional form
Hi all, I find myself struggling to make a shorter version of a form. I am a newb so I apologize in advance for my ignorance. Our web master left for greener pastures and since I'm the only one with any programming experience, I have been forced to take over until we hire a replacement. So… I added a model to set the form-type to "0" or "1" which corresponds to "Long form" and "Short form" When I create a new long form and add the following to the form page template I get the expected answer: {{app.workshop.type.get_form_type_display}} results in "Long form" I'm trying to use this syntax to write an if statement that shows or hides an element based on the form_type. So when I'm in my forms.py doc I include this: if self.workshop.type.form_type == '0': home_address = forms.CharField( label="Home Street Address", required=False, ) Self returns the error "not defined". As does instance, this, etc. the if statement works as such: foo = '0' if foo == '0': home_address = forms.CharField( label="Home Street Address", required=False, ) This code is in a sub class where I'm tying to obtain "workshop.type.form_type" from the parent class and can't figure out how to access it. I have a sinking feeling that the answer is staring me in the face. Thanks in advance for your help, Len -- 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.