My basic question is this: Is there an equivalent of
form = MyForm(request.POST, request.FILES)
when handling file uploads using the webapp
framework on Google App Engine?
I know that I can pull out specific uploaded file data using self.request.get('field_name')
or a FieldStorage object using self.request.params['field_name']
, but in my case I do not know the field names in advance. I would like to let the form object take care of pulling the data it needs out of the request (especially since the forms, and thus the incoming field names, have prefixes).
I'm porting a sort of generic CMS system from Django (already running on App Engine) to Google's webapp framework, and this is the last major stumbling block. When a new item is created, I'd like to be able to just pass the POSTed parameters and files straight to the Django form object for that item, so that I don't have to know ahead of time what fields are in the form and which of those fields are file uploads. This was possible using Django, but I can't see any easy way to do it in the webapp framework.
Have a look at app-engine-patch.
request.FILES
is supposed to work if you use it.There is also a little snippet here, but I have not tested it:
self.request.arguments()
contains all the elements that you could retrieve separately usingself.request.get('field_name')
if you knew 'field_name'.Since you don't know the name of your fields in advance, you might want to loop on all of it's elements. This would work just fine if the names of your fields have a fixed prepend, such as
photo_cat
andphoto_dog
.Here's the solution I came up with. I added the following method to my custom
webapp.RequestHandler
subclass:Now, when handling uploaded files, I can do something like the following:
This works well for me so far.
(I don't know if it's bad form to answer my own question. I'm sorry if it is.)