小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

flask 基礎(chǔ)語法學(xué)習(xí)

 印度阿三17 2019-04-12

回顧

#6行flask
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
    return "HelloWorld!!"
app.run()

1.response

from flask import render_template,redirect,send_file,jsonify
return =httpresponse
render_template
redirect
特殊返回值
1.send_file(文件路徑) 打開文件并返回文件內(nèi)容 Content-Type 文件類型 自動識別
2.jsonify({k:v}) Content-Type:application/json app.config["JSONIFY_MIMETYPE"]

2.request

request.method 請求方式
request.args url參數(shù)
print(request.args.to_dict())#轉(zhuǎn)換成字典
request.form FormData 數(shù)據(jù)
request.json #請求頭 Content-Type:application/json 數(shù)據(jù)序列化至此
request.data 只要請求體中有內(nèi)容 b"”
request.files 接收FormData中的文件

3.session

from flask import session
app.secret_key="123213"
session["user"]="username"

4.路由

動態(tài)參數(shù)
@app.route("/detail/<stu_id>")
def detail(stu_id)
    1.endpoint Flask Mapping 路由和視圖的指向
    2.methods 允許進(jìn)入視圖函數(shù)的請求方式

5.初始化配置

Flask(__name__) 中
1.template_folder 模版存放路徑
2.static_folder 靜態(tài)文件存放路徑
3.static_url_path 金泰文件訪問路徑

6.config對象 正式測試環(huán)境

 2.Flask對象配置
    app.config == app.default_config 查看默認(rèn)配置 及 配置項
    
    class Obj(object):
        DEBUG = True
    
    app.config.from_object(Obj) # 記住

7.藍(lán)圖

Flask藍(lán)圖 
    Blueprint:  bp.py
    from flask import Blueprint
    # 把Blueprint理解為 不能被 Run 的 Flask 對象
    bp = Blueprint("bp",__name__,url_prefix="/user")

    @bp.route("/bp",methods=["GET","Post"])
    def my_bp():
        return "I am bp in user.py"
    
    __init__py
    from flask import Flask
    from .views import add

    def create_app():
        app=Flask(__name__)
        app.register_blueprint(add.add)
        return  app
    
    app.py:
    from flask import Flask
    from user import bp
    from acc import acc
    app = Flask(__name__)

    app.register_blueprint(bp)
    app.register_blueprint(acc)

    if __name__ == '__main__':
        app.run(debug=True)

8.特殊裝飾器 中間件

@app.before_request # 在請求進(jìn)入視圖函數(shù)之前
@app.after_request # 結(jié)束視圖函數(shù)之后,在響應(yīng)返回客戶端之前
def af5(ret):

正常 be1 - be2 - vf - af5 - af4 - af3 - af2 - af1
異常 be1 - af5 - af4 - af3 - af2 - af1

@app.errorhandler(404)
def error404(error_message):

9.CBV

from flask import views

class Login(views.MethodView):
    def get(self):
        pass
    
    def post(self):
        pass

app.add_url_rule("/login",endpoint=None,view_func=Login.as_view(name="login"))

endpoint == as_view(name="login") 中的 "login"

jianjin2 模版

    {{}} 引用 執(zhí)行函數(shù)
    {%%} 邏輯代碼

    {{ stu_list[0].name }}{{ stu_list.0.age }}
    {{ stu_dict }}

{% for stu in stu_list %}
    <p>{{ stu.name }}{{ stu.get("age") }}{{ stu["gender"] }}</p>
{% endfor %}
#if判斷
{% if v["gender"] == "中" %}
    男
{% else %}
    {{ v["gender"] }}
{% endif %}

自定義標(biāo)簽simple_tag

@app.template_global()#全局使用 可選
def ab(a,b):
    return a b
 html中引入
 {{ ab(4,4) }}
 
 前端執(zhí)行html
@app.route("/")
def index():
    tag = "<input type='text' name='user' value='xiao'>"
    return render_template("index.html",tag=tag)
<body>

    {{ tag|safe }}

</body>

后端執(zhí)行html
from flask import Markup  # 導(dǎo)入 flask 中的 Markup 模塊
@app.route("/")
def index():
    tag = "<input type='text' name='user' value='xiao'>"
    # Markup幫助咱們在HTML的標(biāo)簽上做了一層封裝,讓Jinja2模板語言知道這是一個安全的HTML標(biāo)簽
    markup_tag = Markup(tag)
    print(markup_tag,type(markup_tag))

    return render_template("index.html", tag=markup_tag)
    
母板
index.html
<body>
    <h1>Welcome to My</h1>
    <h2>下面的內(nèi)容是不一樣的</h2>
    {% block content %}

    {% endblock %}
    <h2>上面的內(nèi)容是不一樣的,但是下面的內(nèi)容是一樣的</h2>
    <h1>My is Good</h1>
</body>
login.html
{% extends "index.html"%}
{% block content %}
{% endblock %}

include jinja2
login.html
<h4>歡迎登陸</h4>
<form>
    用戶名:<input type="text" name="user">
    密碼:<input type="text" name="pwd">
    <input type="submit" value="提交">
</form>

index.html 文件中的內(nèi)容

<body>

    <h1>Welcome to My</h1>

    {% include "login.html" %}
    <h2>上面的內(nèi)容是不一樣的,但是下面的內(nèi)容是一樣的</h2>

    <h1>My is Good</h1>

</body>

?

來源:http://www./content-4-162701.html

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多