from flask import Flask, request, render_template
import numpy as np
app = Flask(__name__)
@app.route("/")
def home():
return render_template("home.html")
@app.route("/testnpv",methods=["POST"])
def my_npv():
num1 = request.form["num1"]
num2 = request.form["num2"]
num3 = request.form["num3"]
num4 = request.form["num4"]
num5 = request.form["num5"]
npv = np.npv(.5,[float(num1),float(num2),float(num3),float(num4),float(num5)])
return npv
这是html模板
<!DOCTYPE html>
<html>
<head>
<title> FinCalc</title>
<link
rel="stylesheet"
href=https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css
/>
</head>
<body>
<form action="/testnpv" method="POST">
<input type="number"
placeholder="Cash flow one"
class="u-full-width"
name="num1"/>
<input type="number"
placeholder="Cash flow two"
class="u-full-width"
name="num2"/>
<input type="number"
placeholder="Cash flow three"
class="u-full-width"
name="num3"/>
<input type="number"
placeholder="Cash flow four"
class="u-full-width"
name="num4"/>
<input type="number"
placeholder="Cash flow five"
class="u-full-width"
name="num5"/>
<label for="Npv">Npv</label>
<input type="submit" value="calculate" id="calc_btn"/>
我可以返回npv值,但在其他页面上。我希望它在与输入相同的页面上返回npv值。我尝试从my_npv()返回html模板和npv,但收到此错误“ TypeError:render_template()接受1个位置参数,但给出了2个位置”
python和flask的新手。感谢您的帮助!