基本实现过程
应用
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World'
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 5000)
应用主体是Flask(__name__)
route 是路由,表示到了哪一步需要调用哪个函数
必须有主函数,调用run
从 url 接收变量
@app.route('/hello/<name>')
当输入 url / hello / myname 时 name 会直接获取 myname的数据
构建url
从一个界面跳转到另外一个界面
@app.route('/user/<name>')
def hello_user(name):
if name =='admin':
return redirect(url_for('hello_admin'))
else:
return redirect(url_for('hello_guest',guest = name))
需要引入 flask 中的 redirect 和 url_for 模块 ,url_for传入函数名和函数对应需要的参数
html 向 路由发送信息
<html>
<body>
<form action = "http://localhost:5000/login" method = "post">
<p>Enter Name:</p>
<p><input type = "text" name = "nm" /></p>
<p><input type = "submit" value = "submit" /></p>
</form>
</body>
</html>
action 是 路由位置,method 是传输的方式
@app.route('/login',methods = ['POST', 'GET'])
def login():
if request.method == 'POST':
user = request.form['nm']
return redirect(url_for('success',name = user))
else:
user = request.args.get('nm')
return redirect(url_for('success',name = user))
需要从 flask 中导入 request 模块
默认是用request模块接收数据
html 和路由之间传送数据
从 flask 导入模块 render_template 并在主程序同一级建立文件夹templates,所有的html文件放在里面
<html lang="en">
<head>
<meta charset=UTF-8">
<title>Title</title>
</head>
<body>
模板内容
</body>
</html>
示例html
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('login.html')
if __name__ == '__main__':
app.run(debug=True)
<html lang="en">
<head>
<meta charset=UTF-8">
<title>Title</title>
</head>
<body>
<br>
{{ dic }}
<br>
{{ ls }}
<br>
{{ string }}
<br>
{{ num }}
<br>
</body>
</html>
想要接收的变量要写成 {{ name }} 空格,左右括号的格式绝对不能变
@app.route('/')
def index():
dic = {1: 'a', 2: 'b'}
ls = [1, 2, 3]
string = 'nihoa'
num = 12
return render_template('login.html',
dic = dic,
ls = ls,
string = string,
num = num)
传递参数的方式依旧是render_template, 特定参数的传输用 等号
按钮触发事件
<html>
<head>
<script type = "text/javascript" src = "{{ url_for('static', filename = 'hello.js') }}"></script>
</head>
<body>
<input type="button" onclick="sayHello()" value="Say hello"/>
</body>
</html>
按钮有函数onclick
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('login.html')
if __name__ == '__main__':
app.run(debug=True)
function sayHello() {
alter("Hello World")
}
问题:运行时出现get js 404
页面显示表单
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def student():
return render_template('student.html')
@app.route('/result', methods=['POST', 'GET'])
def result():
if request.method == 'POST':
result = request.form
return render_template("result.html", result=result)
if __name__ == '__main__':
app.run(debug=True)
注意在url旁边的是methods复数拼写
<form action = "http://localhost:5000/result" method = "POST">
<p>Name <input type="text" name = "Name" /></p>
<p>Physics <input type="text" name = "Physics" /></p>
<p>Chemistry <input type = "text" name = "chemistry" /></p>
<p>Maths <input type = "text" name = "Mathematics /"/></p>
<p><input type = "submit" value = "submit" /></p>
</form>
input 在 html 语法中不需要反对应,只有一行的话用/就够了
input 有 type 和 name 两种类型,name相当于是关键字,
在form中自动形成了字典
<!doctype html>
<table border = 1>
{% for key, value in result.items() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</table>
tr 表示 在一行 th表示行首,td表示行末
在服务器运行代码
想要在服务器上运行flask代码首先要在服务器上部署flask