将参数传递给瓶子框架函数

问题描述 投票:0回答:1

我正在尝试制作一个简单的Bottle Web应用程序,该应用程序具有一个简单的函数,该函数接受两个参数(两个整数)并返回两个整数的和,但将它们连接起来。我是新手。这是代码:

from bottle import run, get

@get('/')
def docs():
    return {'<h1> Calculator </h1>'}

@get('/calc/<x>/<y>')
def add(x,y):
    return x + y


run(reloader=True, debug=True)

python-3.x bottle
1个回答
0
投票

默认情况下,路径参数中的Bottle返回字符串。您必须将字符串转换为整数对象。瓶子可以帮你做。

尝试一下:

@get('/calc/<x:int>/<y:int>')
def add(x,y):
    return x + y
© www.soinside.com 2019 - 2024. All rights reserved.