python - GET() 采用 1 个位置参数,但已给出 2 个

标签 python web.py positional-argument

我是 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'> at /

GET() takes 1 positional argument but 2 were given

每当我向 GET 函数添加随机参数时,页面就能够运行,但其他情况下就不能运行。如果有人能指出这里出了什么问题,那就太好了。

最佳答案

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

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

关于python - GET() 采用 1 个位置参数,但已给出 2 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53348110/

相关文章:

python - 我怎样才能得到一个循环来忽略列表中的非字母元素?

javascript - web.input() 不接收表单数据

python - Python 错误 : missing 1 required positional argument

python - Vertex AI - 查看管道输出

python - 使用模板时 flask 错误

python - HTML 表格无法在 Outlook 中打开 (pd.to_html)

python - Web.py 在这种情况下如何访问渲染函数

python - pymysql.err.错误 : Already closed

python - Python 3.6 sum() 是否有 `start=0` 关键字参数?