【问题标题】:GET() takes 1 positional argument but 2 were givenGET() 接受 1 个位置参数,但给出了 2 个
【发布时间】:2019-04-20 05:56:09
【问题描述】:

我是 web.py 的新手,我尝试制作一个简单的应用程序,在其中检索 HTML 文件并显示它。

这是我的完整代码:

import web

render = web.template.render('templates/')

urls = (
    '/(.*)', 'index'
)

class index:
    def GET(self):
        return render.index()

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

当我运行它时,我收到错误消息:

<class 'TypeError'>@/

GET() 接受 1 个位置参数,但给出了 2 个

每当我向 GET 函数添加随机参数时,页面都可以正常工作,否则不能正常工作。如果有人能指出这里出了什么问题,那就太好了。

提前致谢。

【问题讨论】:

  • 我不知道什么是 web.py,但可能 wsgi 服务器将请求对象作为第二个参数传递给处理程序
  • 根据 web.py 文档,它适用于 python 2,我下载了 python 3 的实验版本,这可能是原因吗?
  • 好的,我明白了。 /(.*) 表示您要捕获一个路径参数并将其传递给处理程序。

标签: python web.py positional-parameter


【解决方案1】:

(.*) 将用作第二个参数,更改您的代码

class index:
    def GET(self, name):
        return render.index(name)

和模板index.html

$def with (name)
<html>
<head>
    <title>Hello $name</title>
</head>
<body>
Hello $name
</body>
</html>

现在尝试打开http://127.0.0.1:8080/John

【讨论】:

    猜你喜欢
    • 2020-10-30
    • 2016-10-13
    • 2019-07-19
    • 2015-09-01
    • 2018-11-15
    • 2014-07-19
    • 2016-08-09
    • 2020-01-30
    • 2019-08-02
    相关资源
    最近更新 更多