Reputation: 20419
User uploads unknown number of files to my GAE application (photos + their descriptions). The form contains
<input type=hidden name="{{ item.photo_imgsrc_1280 }}" id="file{{ forloop.counter }}">
<input type=hidden name="desc{{ forloop.counter }}" value="{{ item.description }}">
(name value is actually replaced by Picasa with the file content)
Now in the upload handler I need to find according description for each file. Currently I do:
files_counter = 0
for argument in arguments:
if 'localhost' in argument: # choose files only, not other fields - Picasa related approach
files_counter +=1
# self.request.get(argument) here returns the file
# self.request.get('desc'+str(files_counter))) can return some other description, not
# related to the file above
How can I fix that?
Upvotes: 0
Views: 230
Reputation: 13629
You don't need different names for each hidden field. You can have the same name multiple times, and get them all. So let's change your fields a little bit:
<input type=hidden name="{{ item.photo_imgsrc_1280 }}" id="file{{ forloop.counter }}">
<input type=hidden name="photo" value="{{ item.photo_imgsrc_1280 }}">
<input type=hidden name="description" value="{{ item.description }}">
Then, get all values for the photo
and description
fields:
photo_names = self.request.POST.getall('photo')
descriptions = self.request.POST.getall('description')
Here, photo_names
is a list of the field names with photos. To get the actual image fields you'd do something like:
photos = [self.request.POST.get(name) for name in photo_names]
Now, you can group them into a list of tuples (photo, description)
, and iterate one by one:
data = zip(photos, descriptions)
for photo, description in data:
# do what you need with each single photo/desc...
Upvotes: 2