1. 程式人生 > >Flask(十)flash與前臺交互post詳解

Flask(十)flash與前臺交互post詳解

ace for pos pre 視圖 bmi temp 做的 輸入

Project name :Flask_Plan

templates:templates

static:static

POST提交方式,首先要有表單

老實去改模板文件吧。

查詢窗口我準備放在頁面最頂上,就改base.html吧

body中增加

<form action="{{ url_for(‘view_carriage‘) }}" method="get" >
    <input type="text" maxlength="6" name="carriage_num" placeholder="請輸入車號">
    <button type="submit"
>查找車號get</button> </form> <form action="{{ url_for(‘view_carriage‘) }}" method="post" > <input type="text" maxlength="6" name="carriage_num" placeholder="請輸入車號"> <button type="submit">查找車號post</button> </form> <form action="{{ url_for(‘view_plan‘) }}"
method="get"> <input type="text" maxlength="8" name="plan_date" placeholder="請輸入日期"> <button type="submit">查找日期get</button> </form> <form action="{{ url_for(‘view_plan‘) }}" method="post"> <input type="text" maxlength="8" name="plan_date" placeholder="請輸入日期"
> <button type="submit">查找日期post</button> </form>

提交地址{{ url_for(‘view_plan‘) }},就是視圖函數的名稱。在Flask_Plan.py中的。

修改Flask_Plan.py

@app.route(/)
def hello_world():
    return render_template(plan.html)

@app.route(/view_plan/,methods=[GET,POST])     #get或post方式的請求都會響應
def view_plan():                                       #視圖函數
    if request.method ==GET:                         #判斷請求方式為GET
        date = request.args.get(plan_date)          #獲取get提交過來的數據。
        print(date)                                    #後臺打印get提交的數據
        return Plan GET                             #隨意返回一個提示,這是plan視圖的get方法返回
    elif request.method ==POST:
        date=request.form.get(plan_date)
        print(date)
        return Plan POST

@app.route(/view_carriage/,methods=[GET,POST])
def view_carriage():
    if request.method ==GET:
        date = request.args.get(carriage_num)
        print(date)
        return Carriage GET
    elif request.method ==POST:
        date = request.form.get(carriage_num)
        print(date)
        return Carriage POST

這樣就都有了。還有了表單的get方式。

想怎麽用就怎麽用吧,這裏有了後臺的取出方法。

文件上傳以後再寫吧,現在要做的項目,還不用上傳文件

Flask(十)flash與前臺交互post詳解