Exclude form field when calling form from view
I have an image upload form that takes a title and a file for its
field. I have two uses for it. Most of the time I call it, I need both
a title and the image itself. But when I call it simply to grab a
thumbnail, I don't need the title. In fact, the form data is saved to
a different model that doesn't even have title as a field.
I wanted to suppress the "title" field when I call the form. I could
have created two form classes in my forms.py, but this seemed
unnecessarily repetitious.
I included the following in my image form class:
def __init__ (self, show_title=True):
super (BaseClass, self).__init__()
if not show_title:
del self.fields['title']
This works great except for one thing. Now I'm getting validation
errors: "This field is required" whether I suppress the title field or
not. Any time I try to submit the form, it tells me I need to enter
data. Any advice on how to do this correctly?
class UploadFileForm(forms.Form):
def __init__ (self, show_title=True):
super (BaseClass, self).__init__()
if not show_title:
del self.fields['title']
title = forms.CharField(max_length=50)
file = forms.ImageField(label='Select photo to upload')
--
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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home