Flask初级(十)flash与前台交互post详解

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方式。

想怎么用就怎么用吧,这里有了后台的取出方法。

文件上传以后再写吧,现在要做的项目,还不用上传文件

原文地址:https://www.cnblogs.com/jackadam/p/8145716.html