如何避免 Google App Engine 上的 django 中出现 NotImplementedError“仅 tempfile.TemporaryFile 可供使用”?

2024-02-25

我在 Google App Engine 上使用 Django 1.1use_library。这里没有使用 Django GAE helper、Django non-rel 或类似工具。 Django 处理 URL 路由、表单验证等,但我使用的是纯 App Engine 模型。

在我的一个 Django 表单中,有一个 FileField,它有时似乎会调用django.core.files.uploadedfile.TemporaryUploadedFile。这个类然后使用tempfile.NamedTemporaryFile这会导致 App Engine 引发:

File "/base/python_runtime/python_dist/lib/python2.5/tempfile.py", line 45, in PlaceHolder
   raise NotImplementedError("Only tempfile.TemporaryFile is available for use")

试图解决这个问题我采取了来自 Google App Engine Helper for Django(不使用NamedTemporaryFile)将其另存为gae_uploadedfile.py在应用程序目录和我的_djangomain.py_我添加的文件:

from google.appengine.dist import use_library
use_library('django', '1.1')
(...)
import gae_uploadedfile
django.core.files.uploadedfile = gae_uploadedfile

djangomain.py是一个文件,我在其中重定向所有网址 - inapp.yaml I have:

- url: /.*
  script: djangomain.py

但这没有帮助,我仍然遇到这个异常。我做错了什么,在使用 FileField 时是否有其他解决方案可以避免此错误django.forms?


您需要使用以下内容更新 settings.py 文件以更改默认的 Django 行为:

# only use the memory file uploader, do not use the file system - not able to do so on
# google app engine
FILE_UPLOAD_HANDLERS = ('django.core.files.uploadhandler.MemoryFileUploadHandler',)
FILE_UPLOAD_MAX_MEMORY_SIZE = 2621440 # the django default: 2.5MB

更多信息请点击这里:文件上传最大内存大小 http://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FILE_UPLOAD_MAX_MEMORY_SIZE and 上传处理程序 http://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=olddocs#upload-handlers

如果您上传图像,您将受到图像转换等 1MB 配额的限制。配额和限制 http://code.google.com/appengine/docs/python/images/overview.html#Quotas_and_Limits

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何避免 Google App Engine 上的 django 中出现 NotImplementedError“仅 tempfile.TemporaryFile 可供使用”? 的相关文章

随机推荐