2012-03-03 58 views
5

是否有與GAE + Python2.7 + webapp2的AJAX實現相關的任何教程或代碼示例。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)

他RES是Specialscope的例子我的代碼:

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) 

回答

3

堆棧跟蹤表明,你有你的WSGIApplication會URL映射是有一個組,但沒有相應的參數的處理程序。

如果你有

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

,那麼你需要

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

您正在使用預期通過幾年的Python 2.7支持的文檔。如果我處於你的位置,我很想讓應用首先在Python 2.5上工作,然後將端口設置爲2.7。

0

我已經爲GAE + Python2.7這裏AJAX圖片上傳的代碼示例, http://verysimplescripts.blogspot.com/

+0

感謝示例Harendra,但我嘗試上載文件時收到404錯誤。有任何想法嗎? – MaxHash 2012-03-06 00:08:21

+0

你可以發佈你的代碼,包括路線。 – specialscope 2012-03-06 02:23:34

+0

我已將代碼添加到我的問題中。謝謝! – MaxHash 2012-03-06 17:16:18

-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), 
     ])  
+0

protorpc是要走的路,但它是新的,仍然是實驗性的,需要遠遠超過您的代碼片段。這些文檔在https://developers.google.com/appengine/docs/python/tools/protorpc/,但截至今天,這些示例有很多錯誤,並且不清楚,但是如果您調試示例,則可以使其發揮作用你自己。我發佈了更正,以使留言簿示例在Google group for protorpc中工作。 – dansalmo 2012-11-02 18:33:58