Upload files to Google Application Engine using Django

I am using gae with django. I have a project called MusicSite with the following url mapping -

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),
)

In MusicFun there is a MusicSite application with the following codes -

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)

Now that you upload the file using / start and click Submit. I hit a blank page with the following url:

localhost:8000/_ah/upload/ahhnb29nbGUtYXBwLWVuZ2luZS1kamFuZ29yGwsSFV9fQmxvYlVwbG9hZFNlc3Npb25fXxgHDA

These random alphabets vary, but the result is the same. Empty after each download. Someone please help.

Server responses are as follows:

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 -
+3
source share
1 answer

Your boot handler returns 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 , , . , !

+3

Source: https://habr.com/ru/post/1738842/


All Articles