1. 程式人生 > 實用技巧 >數到三就刪除遊戲(python)

數到三就刪除遊戲(python)

flask筆記

1、MVT 和MVC區別?

​ 解耦合

​ 頁面展示、資料操作、邏輯處理進行拆分

2、pip安裝與使用

3、虛擬環境

4、python全域性鎖,所以需要引入多執行緒,一般為核數

5、debug模式下,服務會自動重啟(熱啟動)

6、Flask簡介

​ 基於python的web 微 框架

​ 微 並不是功能弱的意思,而是儘量保持核心程式碼的簡潔,不為使用者做過多的選擇

​ 給開發者更多的選擇空間

​ 請求流程分析:

​ 瀏覽器 ---> 路由器(route) ----> 檢視函式(views) ----> 模型(models)----> 檢視函式 ----> 模板 ----> 瀏覽器

7、flask-script

-- 實現命令列引數接收
-- pip install flask-script
-- 使用app構建manager物件
-- 實現manager啟動專案

from flask import Flask
from flask_script import Manager

app = Flask(__name__)
manager = Manager(app=app)

@app.route('/')
def hello_world():
	return 'Hello World'
@app.route('/hello'):
def hello():
	return render_template('hello.html')
if __name__ == '__main__':
	# app.run(debug=True, port=8000, host='0.0.0.0')
	manager.run()

啟動命令

python manage.py  runserver -r -d -p 9000 -h 0.0.0.0

8、程式碼結構

static 靜態資原始檔 127.0.0.0.1:8000/static/css/common.css 可以直接訪問
templates 模板檔案
預設兩個都可以直接使用,直接使用相對路徑就好

模板渲染:
    render_template()
相當於:
template = Template('<h1>777</h1>')
template.render()

靜態使用,相當於反向解析
url_for('static', filename='comon.css')

9、flask中的引數

  • 都是關鍵字引數
  • 預設標識是尖括號
  • name需要和對應的檢視函式引數名保持一致
  • 引數允許有預設值
    • 如果有,路由中允許不傳參
    • 如果沒有,引數在路由中必須傳遞
  • 預設引數型別是字串
  • 引數語法<converter: var>
  • string/int/float/path/uuid/any

10、請求方法

@app.route('getStuList', methods=['GET','POST'])

11、請求工具:httpie

pip install httpie

12、url_for

​ url_for('函式名', 引數名=value)

@app.route('/url')
def url():
	print(url_for('get_uuid'))
	print(url_for('any', an='d'))
	return '反向解析'

13、Request

args:

​ 接收get請求引數 or 其他請求中路徑引數(url中?之後的引數)

​ 類字典結構,每一項都是一個元組;

​ 元組中包含兩個元素,左側是key,右側是value;

​ key是可以重複的,dict.get('name')只會拿到第一個key為name的元組

​ 獲取方式:dict['name']; dict.get('name'); dict.getlist('name')

from flask import Flask, render_template, request

@app.route('/request', methods=['GET', 'POST'])
def req():
	print(request)
	print(type(request))
	print(request.method)
	print(request.data)
	# arguments 引數 get請求引數
	print(request.args)
    # post相關請求引數
    print(request.form)
    print(request.files)
    print(request.cookies)
    # 請求的遠端地址
    print(request.remote_addr)
    # 瀏覽器身份
    print(request.user_agent)
    print(request.url)
    return 'request請求相關'

14、Response

字串

render_template

make_response

Response

from flask import Flask, render_template, request, url_for, Response
@app.route('/getList')
def get_list():
    response = Response(response='<h2>德瑪西亞</h2>', status=403, content-type='application/json')
    print(response)
    return response

15、響應型別

​ Response物件

​ redirect

​ abort

​ json

返回JSON

jsonify:將資料格式轉化為json格式,同時設定返回值為“application/json”

json.dumps:將資料格式轉化為json格式,沒有設定返回的資料型別,預設型別為“text/html”

16、重定向&終止響應

from werkzeug.exceptions import abort
from werkzeug.utils import redirect
# 重定向
@app.route('/redirect')
def redir():
    response = redirect(url_for('hello'))
    return response


# 終止響應
@app.route('/abort/')
def ab():
    abort(403)
    return

17、問題

pip uninstall flask
pip install Flask==1.1.4

18、藍圖拆分

包和資料夾的區別?是否有'init'

# 安裝外掛
pip install flask-blueprint
# 初始化藍圖
blue = Blueprint('blue', __name__)
# 在app中註冊
app.register_blueprint(blueprint=blue)
# 使用
@blue.route('/rule/')
def hello():
    return 'Hello World'

19、外掛

Flask-script:在終端動態接收命令列引數,傳遞到python程式碼中

flask-blueprint:解決迴圈引用問題,拆分views,代替了用app來註冊路由的問題

20、客戶端會話技術

  • 跨越請求傳遞資料
  • web開發中使用的短連線
  • cookie
    • 客戶端會話技術
    • 資料儲存在瀏覽器中
    • 支援過期
    • 不能跨域
      • frame標籤
      • 可以直接載入整個網站
    • 不能跨瀏覽器
    • cookies是通過Response進行操作
    • flask中的cookie可以直接支援中文
      • flask對內容做了編碼處理
  • session
    • 服務端會話技術
    • 對資料進行資料安全操作
    • 預設在記憶體中
      • 不容易管理
      • 容易丟失
      • 不能多臺電腦寫作
    • flask-session
      • 預設有效期31天

21、redis應用

22、結構標籤

block
{% block **** %}
{% endblock %}

extends
{% extends *** %}

繼承後保留塊中的內容
{{ super() }}

include
{% include *** %}
包含,將其他html包含進來,體現的是由零到一的概念

marco
{% marco hello(name) %}
	{{ name }}
{% endmarco %}
巨集定義,可以在模板中定義函式,在其它地方呼叫

巨集定義可匯入
{% from *** import *** %}

23、反向解析(url_for)

  • 使用在app中
    • url_for('endpoint')
    • endpoint預設是函式的名字
  • 使用在blueprint中
    • url_for('bluename.endpoint')
    • 藍圖名字.函式名
  • 獲取靜態資源路徑
    • url_for('static', filename='path')
    • static 資源
    • path 相對於資源的相對路徑

24、模板

  • 用來生成html頁面

  • 主要包括兩部分

    • 靜態html
    • 模板語法
  • 模板語法

    • {{ var }}
    • {% tag %}
  • 結構標籤

    • block

      • 規劃型標籤
      • 首次出現挖坑
      • 非首次填坑
      • 多次填坑會出現覆蓋,不想覆蓋就用 {{ super() }}
    • extends

    • includes

      • 包含
      • 能用block + extends實現的,儘量別用include
    • macro

      • 巨集定義
      • 可以在html中定義函式
      • 還可以接收引數
      • 通過呼叫函式生成html
      • 支援匯入操作
        • {% from *** import *** %}

25、ORM

將物件的操作轉換為原生SQL

優點:

  • 易用性,可以有效減少重複SQL
  • 效能損耗少
  • 設計靈活,可以輕鬆實現複雜查詢
  • 移植性好

python的ORM(SQLAlchemy)

外掛支援:flask-sqlachemy