【问题标题】:400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'a'"400 错误请求:浏览器(或代理)发送了此服务器无法理解的请求。键错误:'a'"
【发布时间】:2021-06-14 13:14:09
【问题描述】:

下午好!我正在学习烧瓶并尝试进行授权之类的操作,但它向我显示消息“werkzeug.exceptions.BadRequestKeyError:400 错误请求:浏览器(或代理)发送了此服务器无法理解的请求。 KeyError: 'a'"。我试图检查我的代码,但它没有帮助我。如果它不困难或者你,请告诉我问题可能出在哪里。谢谢。

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <div>
        <form action="/" method="POST">
        
            <input type="text" name="a"/>
            <input type="text" name="b" />
            <input type="submit" name="send" value="Send">

        </form>
    </div>
    {% for m in get_flashed_messages() %}
     <div>{{m}}</div>
    {%endfor%}
    {{ Sum }}

    <a href="/login">Login!</a>
</body>
</html>

login.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <div>
        <form action="/" method="POST">
        
            <input type="text" name="a"/>
            <input type="text" name="b" />
            <input type="submit" name="send" value="Send">

        </form>
    </div>
    {% for m in get_flashed_messages() %}
     <div>{{m}}</div>
    {%endfor%}
    {{ Sum }}

    <a href="/login">Login!</a>
</body>
</html>

app.py:

from flask import Flask, render_template, redirect, abort
from flask.globals import request, session
from flask.helpers import flash, url_for

app = Flask(__name__)
app.config['SECRET_KEY'] = 'fddaeefgweef'

@app.route('/', methods=['POST', 'GET'])
def index():
    Sum = ' '
    if len(request.form['a']) > 2:
        flash('completed!')
    else:
        flash('AAAAHHH')
    if request.method == 'POST':
        a = request.form['a']
        b = request.form['b']
        newSum = float(a)+float(b)
        Sum = str(newSum)   
    else:
        Sum = 'err'
    return render_template('index.html', Sum=Sum)



@app.route('/login/', methods=['POST', 'GET'])
def login():
    if 'userLogged' in session:
        return redirect(url_for('profile', username=session['userLogged']))
    elif request.form['nick'] == 'hey' and request.form['password'] == 123:
        session['userLogged'] = request.form['nick']
        return redirect(url_for('profile', username=session['userLogged']))
    return render_template('login.html')


@app.route('/profile/<username>')
def profile(username):
    if 'userLogged' not in session or session['userLogged'] != username:
        abort(401)
    return f'User {username}'

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

【问题讨论】:

    标签: python python-3.x flask


    【解决方案1】:

    这里

    @app.route('/', methods=['POST', 'GET'])
    def index():
        Sum = ' '
        if len(request.form['a']) > 2:
            flash('completed!')
        else:
            flash('AAAAHHH')
        if request.method == 'POST':
            a = request.form['a']
            b = request.form['b']
            newSum = float(a)+float(b)
            Sum = str(newSum)   
        else:
            Sum = 'err'
        return render_template('index.html', Sum=Sum)
    

    您不分青红皂白地从 FORM 数据中询问 a 的值,但在 GET 的情况下这是没有意义的。

    【讨论】:

    • 哦,是的,现在它正在工作。非常感谢!
    猜你喜欢
    • 2018-12-20
    • 1970-01-01
    • 2018-06-21
    • 2021-07-04
    • 2020-07-10
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多