开发者

Using request.FILES in GAE ModelForm

I have a django form using djangoforms.ModelForm. One of the required fields I have is a FileField. I've been trying to figure out how to pass the uploaded file to the form.

I've been trying to use something like:

def post(self):
    form = StudentForm(data=self.request.POST, files=self.request.FILES)

However, I get the following error:

Traceback (most recent call last):
  File "/<appengine_dir>/google_appengine/google/appengine/ext/webapp/_webapp25.py", line 703, in __call__
    handler.post(*groups)
  File "/<project_dir>/app/handlers/students.py", line 22, in post
    form = StudentForm(da开发者_如何学JAVAta=self.request.POST, files=self.request.FILES)
  File "/<appengine_dir>/google_appengine/lib/webob/webob/__init__.py", line 500, in __getattr__
    raise AttributeError(attr)
AttributeError: FILES

I have specifically specified django 1.2, and I have enctype="multipart/form-data" set in my form. Is there anything else that I am missing?

The closest question I could find was Uploading files to App Engine using webapp and Django forms.

In my case, I know the field name in advance and would be OK using a similar method as was posted. However, I was not able to figure it out or make it work.

Your help is appreciated! Thanks!


If we adapt the answer to the other question you mentioned, that would look like this (not tested):

from django.core.files.uploadedfile import SimpleUploadedFile

upload = self.request.params['field_name']
django_files_dict = {'field_name': SimpleUploadedFile(upload.filename,
                                                      upload.file.read())}
form = StudentForm(data=self.request.POST, files=django_files_dict)

Does it work? If not what error message are you getting?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜