开发者社区> 问答> 正文

flask 服务端报错unexpected keyword argument 'num'

代码

from flask import Flask,request


app=Flask(__name__)


@app.route("/<int:num>",methods=["GET"])
def index():

    return "cd",200

if __name__=="__main__":
    app.run()

curl访问

curl http://127.0.0.1:5000/1

日志

Traceback (most recent call last):
  File "/Users/jiangyd/PycharmProjects/untitled16/venv/lib/python3.7/site-packages/flask/app.py", line 2070, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/jiangyd/PycharmProjects/untitled16/venv/lib/python3.7/site-packages/flask/app.py", line 1515, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/jiangyd/PycharmProjects/untitled16/venv/lib/python3.7/site-packages/flask/app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/jiangyd/PycharmProjects/untitled16/venv/lib/python3.7/site-packages/flask/app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
TypeError: index() got an unexpected keyword argument 'num'

展开
收起
水果黄瓜 2021-10-23 20:51:32 755 0
1 条回答
写回答
取消 提交回答
  • 正确代码,参考如下,需要把参数写入函数中

    @app.route("/<int:num>",methods=["GET"])
    def index(num):
        print(num)
        return "cd",200
    
    if __name__=="__main__":
        app.run()
    
    2021-10-25 21:51:07
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
OPEN SOURCE IN A DATA-DRIVEN WORLD 立即下载
Lambda Processing for Near Time Search Indexing 立即下载
Data Pre-Processing in Python: 立即下载