【问题标题】:GAE + Python2.7 + webapp2 + AJAXGAE + Python2.7 + webapp2 + AJAX
【发布时间】:2012-03-21 22:58:39
【问题描述】:

有没有关于GAE + Python2.7 + webapp2的AJAX实现的教程或代码示例。

我已尝试按照以下说明进行操作:

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

但我收到以下错误:

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)

这里还有另一个类似的讨论:

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

这是我在 Specialscope 示例中的代码:

ma​​in.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)

【问题讨论】:

    标签: ajax google-app-engine python-2.7 webapp2


    【解决方案1】:

    问题出在这里:

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

    你不能使用 webapp2.WSGIApplication 来构建你的应用程序,它不理解 protorpc。而是这样做:

    from protorpc.wsgi import service
    
    app = service.service_mappings([
          ('/upload',                     FileuploadHandler),
          ('/download',                   FileDownloadHandler),
          ])     
    

    【讨论】:

    • protorpc 是要走的路,但它是新的,仍然是实验性的,需要的远不止你的代码 sn-p。文档在这里developers.google.com/appengine/docs/python/tools/protorpc,但截至今天,这些示例有很多错误并且不清楚,但是如果您自己调试示例,您可以使其工作。我已经发布了我为让留言簿示例在 google 组中为 protorpc 工作所做的更正。
    【解决方案2】:

    堆栈跟踪表明您的 WSGIApplication 中有一个 url 映射,其中包含一个组,但没有具有相应参数的处理程序。

    如果你有

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

    那么你需要

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

    您使用的文档比 Python 2.7 支持早了几年。如果我处于你的位置,我会很想先让应用程序在 Python 2.5 上运行,然后移植到 2.7。

    【讨论】:

      猜你喜欢
      • 2012-03-18
      • 2012-07-14
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 1970-01-01
      • 2013-06-29
      相关资源
      最近更新 更多