亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在同一個函數python flask中渲染表單和接受值

如何在同一個函數python flask中渲染表單和接受值

米脂 2022-12-20 14:24:34
在 flask 中有什么方法可以呈現表單并單擊提交按鈕獲取值。我得到的錯誤是 404 未找到,我不知道為什么。有沒有更好的方法來做到這一點。@application.route('/cargohandled',methods=['POST'])def cargohandled():    if request.method == 'post':        postman = request.form['postman']        postype = request.form['postype']        return {'postman' : postman, 'postype': postype}    else:        cursor = connection.cursor()        cursor.execute("SELECT DISTINCT `PortName` From trarep")        portname = cursor.fetchall()        cursor.execute("SELECT DISTINCT `PortType` From trarep")        porttype = cursor.fetchall()        return render_template('cargo_handled.htm.j2',title = 'Cargo Handled',portname = portname,porttype= porttype)    return "CG"
查看完整描述

1 回答

?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

您需要同時允許 GET 和 POST。

你應該在你的if條款中將“post”大寫。


您可以取消該else:子句,因為如果request.method != 'POST'.


finalreturn永遠不會被執行,所以你也可以忽略它。


@application.route('/cargohandled',methods=['GET','POST'])

def cargohandled():

    if request.method == 'POST':

        postman = request.form['postman']

        postype = request.form['postype']

        return {'postman' : postman, 'postype': postype}

    cursor = connection.cursor()

    cursor.execute("SELECT DISTINCT `PortName` From trarep")

    portname = cursor.fetchall()

    cursor.execute("SELECT DISTINCT `PortType` From trarep")

    porttype = cursor.fetchall()

    return render_template('cargo_handled.htm.j2',title = 'Cargo Handled',

                           portname = portname,porttype= porttype)


查看完整回答
反對 回復 2022-12-20
  • 1 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號