Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GAE + Python2.7 + webapp2 + AJAX

Are there any tutorials or code examples related to AJAX implementation for GAE + Python2.7 + webapp2.

I have tried to follow instructions below:

http://code.google.com/appengine/articles/rpc.html

but I receive the following error:

Traceback (most recent call last):
  File "E:\dev\workspace\test\webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "E:\dev\workspace\test\webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "E:\dev\workspace\test\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "E:\dev\workspace\test\webapp2.py", line 1101, in __call__
    handler = self.handler(request, response)
TypeError: __init__() takes exactly 1 argument (3 given)

There is another similar discussion here:

Google App Engine Python Protorpc Error: __call__() takes exactly 1 argument (3 given)

heres is my code from Specialscope's example:

main.py

from BaseHandler import BaseHandler  
from google.appengine.ext import blobstore  
from google.appengine.ext.webapp import blobstore_handlers  
import logging  
from google.appengine.api import files  
from google.appengine.api import images  
import json  
import webapp2

class FileuploadHandler(BaseHandler):  
   def get(self):  
     blobstore.create_upload_url('/static')  
     context={}  
     self.render_response("uploader.html",**context)  
class FileDownloadHandler(blobstore_handlers.BlobstoreUploadHandler,BaseHandler):  
   def post(self):  
     upload_files=self.request.POST  
     #image=upload_files['file']  
     logging.error(upload_files)  
     keys=upload_files.keys()  
     imageurls=[]  
     for key in keys:  
       if key.find("uploadimage")!=-1:  
         image=upload_files[key]  
         file_name=files.blobstore.create(mime_type='image/jpg')  
         with files.open(file_name,'a') as f:  
           f.write(image.value)  
         files.finalize(file_name)  
         blob_key=files.blobstore.get_blob_key(file_name)  
         imageurls.append(images.get_serving_url(blob_key))  
     context={}  
     context['imagelinks']=imageurls  
     self.response.write(json.dumps(context))  

app = webapp2.WSGIApplication([
      ('/upload',                     FileuploadHandler),
      ('/download',                   FileDownloadHandler),
      ], debug = True)     

BaseHandler.py

import webapp2
import os

from webapp2_extras import jinja2
from google.appengine.ext import db


class BaseHandler(webapp2.RequestHandler):

    @webapp2.cached_property
    def jinja2(self):
        # Returns a Jinja2 renderer cached in the app registry.
        return jinja2.get_jinja2(app=self.app)

    def render_response(self, _template, **context):
        # Renders a template and writes the result to the response.
        rv = self.jinja2.render_template(_template, **context)
        self.response.write(rv)
like image 329
MaxHash Avatar asked Nov 05 '22 04:11

MaxHash


1 Answers

The stack trace suggests that you have a url mapping in your WSGIApplication that has a group in it, but there's no handler with the corresponding arguments.

If you have

(r'/foo/(\s+)/(\s+)', FooHandler),

then you need

class FooHandler(webapp2.RequestHandler):
  def get(self, arg1, arg2):
    ...

The doc you're using pre-dates Python 2.7 support by several years. Were I in your position, I'd be tempted to get the app working first on Python 2.5, then port to 2.7.

like image 200
Dave W. Smith Avatar answered Nov 11 '22 04:11

Dave W. Smith