0

我从来没有用 AJAX 做过任何事情,只用 javascript 做一些基本的事情,而且我的 python 经验更偏向于科学方面。也就是说,我在谷歌应用引擎上的网站开发进展顺利,直到我想创建一个 ajax 功能演示。

我在 GAE 网站上找到了这个演示,但它似乎不起作用。这篇文章可能会很长,因为我想解释我遇到的障碍。非常感谢可以提供的任何帮助或见解,无论是关于如何修复此演示或使用不同的 ajax 方法

作为参考,我正在使用带有 Python 2.7 的 Google App Engine,并且正在运行 32 位 Ubuntu 12.04。另外,我知道围绕这个主题提出了其他问题,但我没有找到满意的答复。

...

因此,从演示开始,我将必需index.htmlmain.py、 和app.yaml文件复制到一个名为get. 根据说明,我还创建了一个子目录,static我在其中放入了一个副本json2.js(演示中说可以在json.org找到,但我只能在github找到)。所以我的项目结构是这样的:

-dir:get
  -app.yaml
  -main.py
  -index.html
  -dir static:
    -json2.js

完成所有设置后,我启动了我的网络服务器:

chris@thinkpad:~/code/get$ python ~/google_appengine/dev_appserver.py ./

这成功启动。但是,当我将浏览器指向http://localhost:8080/我收到导入错误时:

File "/home/chris/code/get/main.py", line 6, in <module>
from django.utils import simplejson
ImportError: No module named django.utils

所以我做了一点挖掘,似乎教程是为 python 2.5 编写的,所以我需要对我的app.yaml文件进行一些更改。演示版本如下所示:

application: get
version: 1
runtime: python
api_version: 1

handlers:
- url: /static
  static_dir: static

- url: /.*
  script: main.py

我的新 2.7 版本如下所示。注意参数的添加、 tothreadsafe: true的转换和参数的添加。main.pymain.applibraries

application: get
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /static
  static_dir: static

- url: /.*
  script: main.app

libraries:
- name: django
  version: latest

同样,我成功启动了应用服务器。现在,指向会http://localhost:8080/引发以下错误:

ImportError: <module 'main' from '/home/chris/code/get/main.pyc'> has no attribute app

这导致我查看app底部的函数main.py。默认情况下,它看起来像这样:

def main():
    app = webapp.WSGIApplication([
        ('/', MainPage),
        ('/rpc', RPCHandler),
        ], debug=True)
    util.run_wsgi_app(app)

if __name__ == '__main__':
    main()

按摩它以使其对 python27 更友好一点,将整个代码块、函数和所有内容替换为:

app = webapp.WSGIApplication([
        ('/', MainPage),
        ('/rpc', RPCHandler),
        ], debug=True)

重新启动应用服务器并再次指向http://localhost:8080/,我终于得到了页面出现:

在此处输入图像描述

不幸的是,传奇还在继续。我重新启动应用服务器,得到这个打印输出:

chris@thinkpad:~/code/get$ python ~/google_appengine/dev_appserver.py ./
INFO     2013-05-25 16:18:28,097 sdk_update_checker.py:244] Checking for updates to the SDK.
INFO     2013-05-25 16:18:28,322 sdk_update_checker.py:272] The SDK is up to date.
INFO     2013-05-25 16:18:28,354 api_server.py:153] Starting API server at:  http://localhost:56101
INFO     2013-05-25 16:18:28,375 dispatcher.py:164] Starting server "default" running at: http://localhost:8080
INFO     2013-05-25 16:18:28,385 admin_server.py:117] Starting admin server at: http://localhost:8000

然后我单击“添加”按钮。这会引发一个错误,我已将其全部打印在下面,我无法弄清楚:

INFO     2013-05-25 16:18:37,633 server.py:585] default: "GET / HTTP/1.1" 200 3597
INFO     2013-05-25 16:18:37,989 server.py:585] default: "GET /static/json2.js HTTP/1.1" 304 -
INFO     2013-05-25 16:18:38,164 server.py:585] default: "GET /favicon.ico HTTP/1.1" 404 154
ERROR    2013-05-25 16:18:41,469 webapp2.py:1528] __init__() takes exactly 1 argument (3 given)
Traceback (most recent call last):
  File "/home/chris/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/home/chris/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/home/chris/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/home/chris/google_appengine/lib/webapp2-2.3/webapp2.py", line 1076, in __call__
handler = self.handler(request, response)
TypeError: __init__() takes exactly 1 argument (3 given)
ERROR    2013-05-25 16:18:41,471 wsgi.py:235] 
Traceback (most recent call last):
  File "/home/chris/google_appengine/google/appengine/runtime/wsgi.py", line 223, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/home/chris/google_appengine/lib/webapp2-2.3/webapp2.py", line 1519, in __call__
    response = self._internal_error(e)
  File "/home/chris/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/home/chris/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/home/chris/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/home/chris/google_appengine/lib/webapp2-2.3/webapp2.py", line 1076, in __call__
handler = self.handler(request, response)
TypeError: __init__() takes exactly 1 argument (3 given)
INFO     2013-05-25 16:18:41,479 server.py:585] default: "GET /rpc?action=Add&arg0=%221%22&arg1=%222%22&time=1369498721460 HTTP/1.1" 500 -

在这一点上,我被困住了。任何关于如何让这个演示正常运行的想法,或者可能是让我的网络应用程序 ajaxy 更好的路径,将不胜感激。

4

1 回答 1

2

任何关于如何让这个演示正常运行的想法,或者可能是让我的网络应用程序 ajaxy 更好的路径,将不胜感激。

因为在这里发布一个完整的应用程序有点矫枉过正,我决定在 Github 上放一个非常简单的 ajax 演示。这不会立即解决您的问题,但应该有助于解决这个问题。

于 2013-05-26T02:03:01.770 回答