首页 > 解决方案 > 尝试使用烧瓶构建应用程序,但在提交按钮时出现 404 页面未找到错误

问题描述

我正在尝试使用烧瓶构建应用程序以提交登录按钮,但我收到错误 404 page not found

from flask import Flask, redirect, url_for, request

app=Flask(__name__)
@app.route("/success/<name>")

def success(name):
    return 'welcome %s' % name

@app.route('/login',methods=['POST','GET'])
def login():
    if request.method == 'POST':
        user=request.form['nm']
        return redirect(url_for('success',name=user))
    else:
        user=request.args.get('nm')
        return redirect(url_for("success",name=user))

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

这是 login.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="{{url_for('predict')}}" method="POST">
    <p>Enter Name:</p>
    <p><input type="text" name="nm"/></p>
    <p><input type="submit" value="predict"/> 
    </p>
    </form>
</body>     
</html>

标签: python-3.x

解决方案


  1. 好像您正在尝试将表单数据发布到/predict不存在的端点。尝试将其更改为/login.
  2. 您也需要一些 html 来加载成功页面。您应该使用该render_template()html 的函数并将name其作为变量传递给该函数。然后使用该{{name}}html 中的模板模式来显示结果。

推荐阅读