I've created a dynamic form to allow users to upload images, and I'm
trying to validate the form.  - it displays fine, but when I validate
and return a response, I get either a Key Error at the line (data =
self.cleaned_data[dataName] ) in _clean_photo or I get a render error
in my template at {{ image_form.as_table }} - I'm not too sure why
sometimes I get either of the errors.

In my view that gets posted to, i have:
  #images_submitted is a list of the names of the  submitted image
fields
  listingImages = make_image_form(images_submitted)
  image_form = listingImages(request.POST, request.FILES)

The code calls into the make_image_form  view to create the form:

def _clean_photo(self, dataName):
        data = self.cleaned_data[dataName]
        if data != None:
            if data.size > max_photo_size:
                raise forms.ValidationError("The maximum image size
allowed is 500KB")
            elif data.size == 0:
                raise forms.ValidationError("The image given is
empty.")
        return data

def make_image_form(image_fields):
    ''' Takes a list of image_fields to generate image fields '''
    images = SortedDict()  #a django sortedDict
    #create the ImageFields for the form
    for image_name in image_fields:
        images[image_name] = forms.ImageField(required=False)
    new_form = type('ListingImagesForm2', (forms.BaseForm,),
{'base_fields' : images})
    #now we add the validation methods to the class
    for image_name in image_fields:
        setattr(new_form, 'clean_' + image_name, lambda self:
self._clean_photo(image_name))
    #Add the _clean_photo method to the class
    setattr(new_form, '_clean_photo', _clean_photo)
    return new_form



The _clean_photo method only gets called once -  for the first photo.
The ListingImagesForm2 class gets created fine, and the clean_photo_1,
2, 3 methods get created fine as well.

I'm not too sure what's happening with the errors, but I think that it
must have something to do with how I'm validating.  When _clean_photo
was called,  self.cleaned_data had the image info for the first image
only, when 2 or 3 images were posted.

I hope I've provided enough information for help, any ideas as to what
might be wrong will be highly appreciated :)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to