•Flask 是一個微框架,主要面向需求簡單的小應用。
小應用指的是隻有簡單的2~3個功能的網絡應用等……而 Django更加適合於大型綜合的項目,併爲其提供一站式開發服務。
Flask 讓開發者來抉擇如何存儲數據等操作,隨意性更強。
針對非Django框架的最流行的ORM,Flask目前在數據庫存儲方面採用的是 SQLAlchemy,當然也有很多其他的選擇,比如 DynamoDB和 MongoDB,亦或是像 LevelDB和 SQLite這樣的簡單本地持久化。
Flask 創始與2010年年中,儘管Flask的歷史較短,但它能夠從以前的框架學到一些東西並且將它的目標設定在了小型項目上。
它在一些僅有一兩個功能的小型項目上得到了大量應用。比如 httpbin這樣的項目,簡單但非常強大,是一個幫助 debug和測試 HTTP的庫。
Flask 是一個基於Werkzeug, Jinja 2 以及美好意願構想的 Python 微框架
Flask框架的特點
•Flask 框架開發非常簡潔。
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello ( ):
return "Hello World!"
簡單並快速的安裝、啓動。
> python hello.py
* Running on http://127.0.0.1:5000
接下來用Flask框架快速創建一個簡單的登陸界面,這裏用的頁面是之前自己初學的時候模仿CSDN做的一個網頁,上代碼!
from flask import Flask, render_template, request, session
import config
from config_class import *
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'AAGGCCFFDDEEFFGG'
#使用config。py文件定義配置
app.config.from_object(config)
#使用class定義配置
app.config.from_object(DebugConfig)
@app.route('/') #路由
def index():
return render_template('index.html')
@app.route("/login.do",methods=['POST','GET'])
def login():
userName = request.form.get('userName')#表單提交的數據使用request下面的 form方法
userPwd = request.form.get('userPwd')
if userName == 'zhangsan' and userPwd =='123456':
session['userName'] = 'zhangsan'
return render_template('index.html')
elif userName!=None and (userName!='zhangsan'or userPwd!='123456'):
return render_template('login.html',message='用戶名或密碼錯誤!')
pass
return render_template('login.html')
pass
if __name__ == '__main__': #換成自己的ip地址
app.run(host=app.config['SERBER_ADDR'], port=app.config['SERVER_PORT'], debug=True)
#WSGI
自定義配置文件,
'''
自定義配置文件
'''
DEBUG = True
SECRET_KEY = 'AAAAAAAAAAAAAAAAA'
PAGE_SIZE = 10
SERBER_ADDR = '10.0.14.160'
SERVER_PORT = 80
這裏是.html就是上面說的以前作的一個簡單的網頁,今天加了一個登陸
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
</script>
<style type="text/css">
a{
margin-right: 12px;
}
</style>
</head>
<body style="min-width:1200px;padding: 0px;margin: 0;background-color: lavenderblush;">
<div id="header" style="width: 100%;margin: 0px;padding: 0px;height: 40px;border: solid 0px red;background-color: white; ">
<div style="width: 90%;border: solid 0px red;margin-left: 5%;padding: 6px;" align="left">
<div style="border:solid 0px red;padding: 0px;float: left"></div>
<div style="border:solid 0px red;padding: 0px;float: right">
<a href="/login.do">
{% if session.get('userName') == None %}
登錄
{% elif session.get('userName') != None %}
{{ session.get('userName') }}
{% endif %}
</a></div>
<a>首頁</a><a>首頁</a><a>首頁</a><a>首頁</a><a>首頁</a><a>首頁</a><a>首頁</a><a>首頁</a>
</div>
</div>
<div id='main' style="width: 90%;border: solid 0px red;margin-left: 5%;" align="center">
<div id='left' style="min-height:800px;background-color: white;width: 14%;float:left;border: solid 0px red;margin: 4px;">
<ul>
<li><a href="table_lianliankan.html" target="innerFrame">人工智能</a></li>
<li>人工智能</li>
<li>人工智能</li>
<li>人工智能</li>
<li>人工智能</li>
<li>人工智能</li>
<li>人工智能</li>
<li>人工智能</li>
<li>人工智能</li>
<li>人工智能</li>
<li>人工智能</li>
<li>人工智能</li>
<li>人工智能</li>
</ul>
</div>
<div id='middle' style="min-height:800px;background-color: white;width: 60%;float:left;border: solid 0px red;margin: 4px;">
<div style="width: 100%;">
a
<!--
<iframe border="0" scrolling="no" width="100%" height="800px" name="innerFrame" src="html_form.html"></iframe>
-->
</div>
</div>
<div id='right' style="min-height:800px;background-color: white;width: 24%;float:right;border: solid 0px red;margin: 4px;">
<div style="width: 100%;">aaaa</div>
</div>
</div>
<div style="background-color: #FF0000;position: fixed;left: 8px;top:200px;height: 400px;">
左廣告
</div>
<div style="background-color: #FF0000;position: fixed;right: 8px;top:200px;height: 400px;">
右廣告
</div>
</body>
</html>
最後做一個點擊登陸之後的一個輸入賬號密碼的界面和按鈕!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{{ url_for('static',filename='css/style.css',_external=True) }}"/>
<link rel="stylesheet" href="static/css/style.css"/>
</head>
<body>
<form action="/login.do" method="post">
<table>
<span color="red">{{ message }}</span>
<tr>
<td>
用戶名:<input type="text" name="userName" />
</td>
</tr>
<tr>
<td>
用戶名:<input type="password" name="userPwd" />
</td>
</tr>
<tr>
<td>
<input type="reset" value="重置" />
<input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>
一套流程圖(由於我都pycharm軟件內部選擇虛擬環境出了一點問題,所以我在外面的虛擬環境運行的,運行結果網站需要手動複製到瀏覽器裏):
複製到瀏覽器裏展示:
點擊登陸進入到這樣的一個頁面輸入賬號密碼
點擊提交
剛學做的比較low,歡迎大佬指點!