什么是Flask?
flask是一種基于python,并且依賴于Jinja2(jinja英文直譯為神社)模板引擎和WSGI(Web Server Gateway Interface,即Web服務器網關介面,其規定了web服務器和python web應用/框架之間的標準介面規范)服務的一種微型框架,
模型為:
HTTP客戶端——— WEB服務器————WSGI————Flask
Flask框架——MTV(MVC)
M(models)——模型層:用于資料庫的建模
T(templates)——模板層:用于處理用戶顯示內容
V(views)——視圖層:處理與用戶互動的部分內容
M(models)———模型層:用于資料庫的建模、處理
V(views)——視圖層:用于處理用戶顯示內容
C(controller)——控制器:用于處理與用戶互動部分內容
flask的安裝
Windows:pip install flask
Linux:sudo pip install flask
flask的基本結構
run1.py檔案
from flask import Flask,render_template,url_for
app=Flask(__name__,template_folder='.')#template_folder為當前路徑,如果不加這個引數,默認為同級 templates檔案夾路徑
路由
@app.route('/')
模板
def index():
return render_template('index.html')
除錯
if __name__ == '__main__':
app.run(debug=True,port=5000)#port:埠,默認埠為5000,訪問地址為localhost:5000/
index.html檔案
<html>
<head>
<meta charset="utf-8">
<title>首頁</title>
</head>
<body>
<p>Welcome to pythoy world!</p>
</body>
</html>

現在我們搭建了一個非常簡單的WEB網站,下面進行更深層次的總結
一、帶型別轉化器引數的路由
(1)帶str型引數:
@app.route('/<name>/<age>')
def daicanluyou(name,age):
return '<p>姓名:{},年齡{}</p>'.format(name,age)
運行結果

(2)帶int型引數
@app.route('/<name>/<int:age>')
def daicanluyou(name,age):
return '<p>姓名:{},明年年齡{}</p>'.format(name,age+1)
運行結果:

二、render_template()方法
(1)不帶引數的render_tamplate()
@app.route('/login')
def login():
return render_template('login.html')
login.html檔案
<html>
<head>
<meta charset="utf-8">
<title>登陸頁面</title>
</head>
<body>
用戶名<input type="text/css" size="30" value="請輸入用戶名"><br/>
密 碼<input type="password" size="30">
</body>
</html>
運行結果:

(2)帶引數的render_template()
@app.route('/information/<name>/<age>')
def information(name,age):
return render_template('information.html',name=name,age=age)
information.html
<html>
<head>
<meta charset="utf-8">
<title>Information</title>
</head>
<body>
<p>姓名:{{name}},年齡:{{age}}</p>
</body>
</html>
運行結果:

(3)多引數的render_tamplate()
@app.route('/duocanshu')
def duocanshu():
name='mumu'
age=23
dic1={'bookname':'穆穆的眼睛','price':'99','publisher':'愛心出版社','date':'2020/12/2'}
return render_template('duocanshu.html',params=locals())
duocanshu.html
<html>
<head>
<meta charset="utf-8">
<title>首頁</title>
</head>
<body>
<p>name:{{params.name}}</p>
<p>age:{{params.age}}</p>
{% for key,value in params.dic1.items() %}
<p>{{key}}:{{value}}</p>
{% endfor %}
</body>
</html>
運行結果:

三、模板
@app.route('/ziban')
def ziban():
return render_template('ziban.html')
index.html
<html>
<head>
<meta charset="utf-8">
<title>首頁</title>
</head>
<body>
<p>Welcome to pythoy world!</p>
{% block container %}
<p>這是模板</p>
{% endblock %}
</body>
</html>
ziban.html
{% extends'index.html' %}
{% block container %}
<p>這是子板</p>
{% endblock %}
運行結果


四、控制結構
@app.route('/control')
def control():
usename='mumu'
age=22
return render_template('control.html',params=locals())
control.html
<html>
<head>
<meta charset="utf-8">
<title>首頁</title>
</head>
<body>
{% if params.usename %}
<p>用戶名為:{{params.usename}}</p>
{% else %}
用戶未找到<a href="{{url_for('login')}}">回傳登陸界面</a>
{% endif %}
</body>
</html>
五、訪問方式
(1)POST
@app.route('/methods',methods=['POST'])
def methods():
return render_template('index.html')
運行結果

網頁源代碼:

(2)POST\GET
@app.route('/methods',methods=['POST','GET'])
def methods():
return render_template('index.html')
運行結果:

六、自定義錯誤頁面
@app.errorhandler(404)
def fail(e):
return render_template('404.html'),404
404.html
{% extends 'index.html' %}
{% block container %}
<li> 您想要的網頁暫時未找到</li>
{% endblock %}
運行結果:

七、過濾器
capitalize :首字符變大寫,其他字符變小寫
lower:轉換成小寫
upper:轉換成大寫
title:每個單詞首字符變大寫
trim:去值兩邊的空格
@app.route('/duocanshu')
def duocanshu():
name='mumu'
age=23
usename='welcome to python'
dic1={'bookname':'穆穆的眼睛','price':'99','publisher':'愛心出版社','date':'2020/12/2'}
return render_template('duocanshu.html',params=locals())
duocanshu.html
<html>
<head>
<meta charset="utf-8">
<title>首頁</title>
</head>
<body>
<p>name:{{params.name}}</p>
<p>age:{{params.age}}</p>
<p>usename:{{params.usename}}</p>
<p>修改后的:{{params.usename|upper}}</p>
<p>修改后的:{{params.usename|title}}</p>
<p>修改后的:{{params.usename|trim}}</p>
{% for key,value in params.dic1.items() %}
<p>{{key}}:{{value}}</p>
{% endfor %}
</body>
</html>
運行結果:

八、靜態檔案
@app.route('/image')
def image():
return render_template('image.html')
image.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>圖片</title>
</head>
<body>
<div>
<img src="/static/images/1.jpg" width=800px>
<img src="{{url_for('static',filename='images/1.jpg')}}">
</div>
</body>
</html>
運行結果:
九、注意WARNING
(1)app=Flask(name,template_folder=’.’):
template_folder表示的是當前路徑可以訪問render_tamplate(‘檔案名’),當html檔案與run1.py處于同一路徑下時,可以正常運行,如果不帶入這個實參,則render_template默認訪問的是根目錄下的tamplates檔案夾下的html檔案,一般在實際開發程序中,不推薦使用template_folder實參,
(2)url_for()
此方法在實際開發程序中非常實用,常用于超鏈接,
(3)靜態檔案
沒有和服務器互動的檔案叫做靜態檔案,在flask應用中,必須將靜態檔案放置于更目錄下的static檔案夾下,否則無法進行正常訪問,

(4)自定義錯誤頁面
在實際開發程序中,如果沿用瀏覽器的錯誤頁面,會降低用戶體驗,則使用自定義的錯誤頁面會更加吸參考戶,如百度,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/229464.html
標籤:python
下一篇:python實作二維圖制作
