快速复习上手路线
轻量级 后端框架 1.flask 路由 用来匹配url 2.request对象 abort函数 3.模板 4.flask数据库 6.表单 7.ajax
一,你好,上进小菜猪
from flask import Flask
app=Flask(__name__)
@app.route('/')
def index():
return '上进小菜猪'
app.run()
from flask import Flask
app=Flask(__name__)
@app.route('/')
def index():
return '<h1>上进小菜猪</h1>'
app.run()
二,路由
from flask import Flask
app = Flask(__name__)
@app.route('/hi',methods=['POST'])
def hi():
return '你好'
@app.route('/hello',methods=['GET','POST'])
def hello():
return '上进小菜猪'
if __name__ == '__main__':
app.run()
from flask import Flask
app = Flask(__name__)
@app.route('/hello',methods=['GET','POST'],endpoint='hello')
def hi():
return '你好'
@app.route('/hi',methods=['GET','POST'],endpoint='hi')
def hello():
return '上进小菜猪'
if __name__ == '__main__':
app.run()
三,路由变量名
from flask import Flask
app = Flask(__name__)
# @app.route('/hello',methods=['GET','POST'],endpoint='hello')
# def hi():
# return '你好'
#
# @app.route('/hi',methods=['GET','POST'],endpoint='hi')
# def hello():
# return '上进小菜猪'
@app.route('/user/<id>')
def index(id):
if id=="1":
return "python"
if id=="2":
return "django"
if id=="3":
return "flask"
return 'nh'
if __name__ == '__main__':
app.run()
四,自定义转换器
#自定义转换器
from flask import Flask
from werkzeug.routing import BaseConverter
app=Flask(__name__)
class RegexConverter(BaseConverter):
def __init__(self,url_map,regex):
#d调用父类方法
super(RegexConverter,self).__init__(url_map)
self.regex=regex
def to_python(self,value):
#父类的方法,功能实现完成
print('111')
return value
app.url_map.converters['re']=RegexConverter
@app.route('/index/<re("123"):vlaue>')
def index(vlaue):
print(vlaue)
return 'hello'
app.run()
五,渲染form表单
py文件
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/index',methods=['GET','POST'])
def index():
return render_template('index.html')
if __name__== '__main__':
app.run()
html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post">
账号:
<input type="text" name="name">
<br>
密码:
<input type="password" name="password">
<input type="submit" name="submit">
</form>
</body>
</html>
六,request对象
from flask import Flask,render_template,request
app = Flask(__name__)
@app.route('/index',methods=['GET','POST'])
def index():
if request.method == 'GET' :
return render_template('index.html')
if request.method == 'POST' :
name = request.form.get('name')
password=request.form.get('password')
print(name,password)
return "post"
if __name__== '__main__':
app.run()
七,重定向
from flask import Flask,redirect,url_for
app= Flask(__name__)
@app.route('/index')
def index():
return redirect(url_for("hello"))
@app.route('/')
def hello():
return '上进小菜猪'
if __name__== "__main__":
app.run()
八,返回json数据给前端
from flask import Flask,make_response,json,jsonify
app=Flask(__name__)
app.config['JSON_AS_ASCII']=False
@app.route('/index')
def index():
data ={
'name':'上进小菜猪'
}
# response=make_response(json.dumps(data,ensure_ascii=False))
# response.mimetype='application/json'
# return response
return jsonify(data)
app.run()
九,abort函数
from flask import Flask,abort,request,make_response,render_template
app=Flask(__name__)
@app.route('/index',methods=['GET','POST'])
def index():
if request.method =='GET':
return render_template('index.html')
if request.method =='POST':
name = request.form.get('name')
password= request.form.get('password')
if name=="xiaozhu" and password =='123':
return '登录成功'
else:
abort(404)
return None
if __name__=='__main__':
app.run()
十,自定义异常
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<img src="../static/r4041.png" alt="" width="900px" height="900px">
</body>
</html>
from flask import Flask,abort,request,make_response,render_template
app=Flask(__name__)
@app.route('/index',methods=['GET','POST'])
def index():
if request.method =='GET':
return render_template('index.html')
if request.method =='POST':
name = request.form.get('name')
password= request.form.get('password')
if name=="xiaozhu" and password =='123':
return '登录成功'
else:
abort(404)
return None
@app.errorhandler(404)
def handle_404_error(err):
return render_template("404.html")
if __name__=='__main__':
app.run()
十一,模板的基本使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ data}}
<br>
{{ data['name']}}
<br>
{{ data.mylist}}
<br>
count:{{ data.mylist[1]+data.mylist[2] }}
</body>
</html>
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/index')
def index():
data = {
'name':'上进小菜猪',
'age':'19',
'mylist':[12,21,32,4],
}
return render_template('index2.html',data=data)
app.run()
十二,过滤器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ data}}
<br>
{{ data['name']}}
<br>
{{ data.mylist}}
<br>
count:{{ data.mylist[1]+data.mylist[2] }}
<br>
{{ 'hello_world' | replace('nj') }}
</body>
</html>
十三,自定义过滤器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ data}}
<br>
{{ data['name']}}
<br>
{{ data.mylist}}
<br>
count:{{ data.mylist[1]+data.mylist[2] }}
<br>
{{ data.mylist | li2}}
</body>
</html>
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/index')
def index():
data = {
'name':'上进小菜猪',
'age':'19',
'mylist':[12,21,32,4,1,43],
}
return render_template('index2.html',data=data)
def list_step(li):
return li[::2]
#注册过滤器
app.add_template_filter(list_step,'li2')
app.run()
|