首页 > 解决方案 > 如何摆脱 BadRequestKeyError?

问题描述

我正在使用 Python 3。我已经创建了必要的文件夹。即静态和模板文件夹。尽管如此,此代码仍无法正常工作。我收到以下错误werkzeug.exceptions.BadRequestKeyError

abc.py

    from flask import Flask, request, render_template, redirect
    app=Flask(__name__)

    @app.route('/project/', methods=['GET', 'POST'])
    def tryy():
        name=request.method
        name1=request.form["uname"]
        return render_template("project1.html",name=name1)

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

项目.html

    <!DOCTYPE html>

    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
            <form action="" method="GET">
                <input type="text" name="uname" />
                <input type="submit" value="Signup" />
            </form>
        </body>
    </html>

项目1.html

    <!DOCTYPE html>

    <html>
        <head>
            <meta charset="utf-8" />
            <title></title>
            <link rel="stylesheet" type="text/css"
                  href="{{url_for('static', filename='style.css')}}" />
        </head>
        <body>
            <h1>Hey there {{name}}. Nice to meet you</h1>
        </body>
    </html>

标签: python-3.xflaskjinja2

解决方案


您的表单操作设置为“”,将其更改为: <form action="/project/" method="GET">


推荐阅读