如何避免NotImplementedError“只有tempfile.TemporaryFile可供使用”在Google App Engine的django上? [英] How to avoid NotImplementedError "Only tempfile.TemporaryFile is available for use" in django on Google App Engine?

查看:389
本文介绍了如何避免NotImplementedError“只有tempfile.TemporaryFile可供使用”在Google App Engine的django上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 use_library 在Google App Engine上使用Django 1.1。没有Django GAE帮手,Django非rel或类似的工具在这里使用。 Django处理URL路由,表单验证等,但我使用纯App Engine模型。$ ​​b
$ b

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

 文件/ base /python_runtime/python_dist/lib/python2.5/tempfile.py,第45行,放置在PlaceHolder中
raise NotImplementedError(只有tempfile.TemporaryFile可供使用)

试着解决这个问题,我把 uploadedfile module c $ c> NamedTemporaryFile )将它作为 gae_uploadedfile.py 保存在应用程序目录和我的 _djangomain.py _ file我添加了:

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

djangomain.py 是一个文件

   -  url:/.*在这里我重定向所有的url  - 在 app.yaml  
script:djangomain.py

但它没有帮助,我仍然得到这个异常。我做错了什么,有没有其他的解决方案,以避免这个错误,同时使用FileField从 django.forms

解决方案

您需要更新settings.py文件以更改默认的Django行为:

 #仅使用内存文件上传器,不要使用文件系统 - 无法在
#google应用程序引擎
上执行此操作FILE_UPLOAD_HANDLERS =('django.core.files.uploadhandler.MemoryFileUploadHandler' ,)
FILE_UPLOAD_MAX_MEMORY_SIZE = 2621440#django默认值:2.5MB

更多信息:< a href =http://docs.djangoproject.com/zh/dev/ref/settings/#std:setting-FILE_UPLOAD_MAX_MEMORY_SIZE =noreferrer> FILE_UPLOAD_MAX_MEMORY_SIZE 和
上传处理程序



如果您上传图片,您将受到1的限制用于图像转换的MB配额等。 Quotas_and_Limits


I'm using Django 1.1 on Google App Engine through use_library. No Django GAE helper, Django non-rel or similar tools are used here. Django handles URLs routing, forms validation etc., but I'm using pure App Engine models.

In one of my Django forms there is a FileField, which from time to time, seems to call django.core.files.uploadedfile.TemporaryUploadedFile. This class then uses tempfile.NamedTemporaryFile and this results in App Engine raising:

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

Trying to solve this problem I took uploadedfile module from Google App Engine Helper for Django (which doesn't use NamedTemporaryFile) saved it as gae_uploadedfile.py in application directory and in my _djangomain.py_ file I added:

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

djangomain.py is a file where i redirect all urls - in app.yaml I have:

- url: /.*
  script: djangomain.py

But it didn't help, I still get this exception. What am I doing wrong, is there other solution to avoid this error while using FileField from django.forms?

解决方案

You need to update the settings.py file with the following to change the default Django behaviour:

# 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

More info here:FILE_UPLOAD_MAX_MEMORY_SIZE and upload-handlers

If you are uploading images you will be restricted by the 1MB quotas for image transformation etc.. Quotas_and_Limits

这篇关于如何避免NotImplementedError“只有tempfile.TemporaryFile可供使用”在Google App Engine的django上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆