首页 > 解决方案 > HTML Python/Flask 传递值 (int)

问题描述

sum_test.py

def sum_t(a, b):

    return a+b

主文件

@app.route('/test', methods=["GET", "POST"])
    def print_test():
        if request.method == "POST":
            # getting input with name = aa in HTML form
            a = request.form.get("aa")
            # getting input with name = bb in HTML form
            b = request.form.get("bb")
            result = sum_test.sum_t(a, b)
            return str(result)
        return render_template('test.html')

结果是

1 + 1 = 11

但我想要的结果

1 + 1 = 2

标签: pythonhtml

解决方案


这个答案应该是不言自明的:)

a = request.form.get("aa", type = int)

您需要将表单字段作为 int 值获取,以便将它们作为数学表达式进行操作。


推荐阅读