【问题标题】:web.py doesnt detect GET methodweb.py 没有检测到 GET 方法
【发布时间】:2014-05-12 08:44:38
【问题描述】:

我是 python 和 web.py 世界的菜鸟。

我刚刚创建了三个文件

urls.py

url_mappings = (
    '/', 'index'
)

index.py

class index:
    def GET(self):
        return "<h1>Hello World</h1>"

    def POST (self):
        return "<h1>Hello World in POST</h1>"

webstart.py

import web
import index
from urls import url_mappings

if __name__ == "__main__":
    app = web.application (url_mappings, globals())
    app.run()

我使用 python webstart.py 启动我的开发服务器,当我点击 localhost:8080 时,它向浏览器发送 None 并且在日志中我看到以下内容

127.0.0.1:52822 - - [19/Mar/2013 20:44:18] “HTTP/1.1 GET /” - 405 方法不允许 127.0.0.1:52822 - - [19/Mar/2013 20:44:18] “HTTP/1.1 GET /favicon.ico” - 404 未找到

我错过了什么?

【问题讨论】:

  • web.py 的“用户指南”中没有介绍这些基本内容吗?
  • 在用户指南 (webpy.org/docs/0.3/tutorial#starting) 中,他们将所有代码放在一个文件中,我试过了,它可以工作。但是当我将它们全部保存在三个不同的文件中时,它就不起作用了:(
  • 今天重要的一课 - 一次只改变一件事 - 在这种情况下,订单是一个问题吗?

标签: python web.py


【解决方案1】:

web.py 找不到控制器类,你应该改变 urls.py:

url_mappings = (
    '/', 'index.index'
)

或在 webstart.py 中导入 index

from index import index

即您应该将 url 直接映射到 module_name.class_nameimport class_name from module_name 以便 class_name 在全局范围内可用。

【讨论】:

    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    相关资源
    最近更新 更多