gpt4 book ai didi

django - 使用 Django 在 Google 应用程序引擎中上传文件

转载 作者:太空宇宙 更新时间:2023-11-03 15:34:50 25 4
gpt4 key购买 nike

我正在将 gae 与 django 一起使用。我有一个名为 MusicSite 的项目,具有以下 url 映射-

urls.py

from django.conf.urls.defaults import *
from MusicSite.views import MainHandler
from MusicSite.views import UploadHandler
from MusicSite.views import ServeHandler

urlpatterns = patterns('',(r'^start/', MainHandler),
(r'^upload/', UploadHandler),
(r'^/serve/([^/]+)?', ServeHandler),
)

MusicFun 中有一个应用程序 MusicSite,其内容如下代码-

views.py

import os
import urllib

from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

def MainHandler(request):
response=HttpResponse()
upload_url = blobstore.create_upload_url('http://localhost:
8000/upload/')
response.write('<html><body>')
response.write('<form action="%s" method="POST"
enctype="multipart/form-data">' % upload_url)
response.write("""Upload File: <input type="file"
name="file"><br> <input type="submit"
name="submit" value="Submit"> </form></body></html>""")
return HttpResponse(response)

def UploadHandler(request):
upload_files=request.FILES['file']
blob_info = upload_files[0]
response.redirect('http://localhost:8000/serve/%s' %
blob_info.key())

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
resource = str(urllib.unquote(resource))
blob_info = blobstore.BlobInfo.get(resource)
self.send_blob(blob_info)

现在每当使用/start 上传文件时单击提交我被带到一个空白页面,其中包含以下 url-

localhost:8000/_ah/upload/ahhnb29nbGUtYXBwLWVuZ2luZS1kamFuZ29yGwsSFV9fQmxvYlVwbG9hZFNlc3Npb25fXxgHDA

这些随机字母不断变化,但结果是一样的。一片空白每次上传后的页面。有人请帮忙。

服务器响应如下-

INFO:root:"GET /start/ HTTP/1.1" 200 -
INFO:root:"GET /favicon.ico HTTP/1.1" 404 -
INFO:root:Internal redirection to http://localhost:8000/upload/
INFO:root:Upload handler returned 500
ERROR:root:Invalid upload handler response. Only 301, 302 and 303
statuses are permitted and it may not have a content body.
INFO:root:"POST /_ah/upload/
ahhnb29nbGUtYXBwLWVuZ2luZS1kamFuZ29yGwsSFV9fQmxvYlVwbG9hZFNlc3Npb25fXxgCDA
HTTP/1.1" 500 -
INFO:root:"GET /favicon.ico HTTP/1.1" 404 -

最佳答案

您的上传处理程序返回 500:

INFO:root:Upload handler returned 500
ERROR:root:Invalid upload handler response. Only 301, 302 and 303 statuses are permitted and it may not have a content body.

这几乎可以肯定是因为它抛出了异常;您需要说服 Django 记录异常,以便您可以看到出了什么问题。或者,自己捕获并记录!

关于django - 使用 Django 在 Google 应用程序引擎中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2532259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com