Friday, February 24, 2012

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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment