from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
'''
加法器
'''
@app.route('/', methods=['GET', 'POST'])
def index():
return redirect(url_for('hello_world'))
# 在url_for中指定方法的名字
# redirect为重定向 而render_template属于模板的渲染
# 重定向(redirec)为把url变为重定向的url,而模板渲染(render_template)则不会改变url,
# 模板渲染是用模板来渲染请求的url ,并且两个不能同时使用
# 访问http://127.0.0.1:5000时直接定位到http://127.0.0.1:5000/add,这是重定向的效果
# 模板渲染,直接返回指定的html,不会改变浏览器地址栏中的链接
# 目前还没想到这个redirect要用在什么业务场景下
# 传递参数
@app.route('/add', methods=['GET', 'POST'])
def hello_world():
if request.method == 'POST':
a = request.form['adder1']
b = request.form['adder2']
a = int(a)
b = int(b)
return render_template('index.html', message=a+b)
return render_template('index.html')
if __name__ == '__main__':
app.run(port=8080)
index.html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>加法</title>
</head>
<body>
<div align="center" style="margin-top: 40px;">
{{ message }}
<form name="form1" method="POST">
<input type="text" placeholder="adder" name="adder1">+
<input type="text" placeholder="adder-2" name="adder2">=
<input type="text" readonly="readonly" placeholder="result" name="result" value="{{ message }}">
<input type="submit" value="计算" onclick="">
</form>
</div>
</body>
</html>
学习完刚才的内容,发现在计算出相加的结果之后,要刷新界面,才能将结果显示出来,这样,两个加数的文本框里的数字就没有了,如何做成一个动态的呢,目前了解到的js可以把,flask可以动态修改网页中的值吗
需要学的还有很多啊