400 Bad Request:浏览器(或代理)发送了一个该服务器无法理解的请求。密钥错误:'a'”

时间:2021-06-14 13:14:09

标签: python python-3.x flask

下午好!我正在学习烧瓶并尝试进行类似授权的操作,但它向我显示消息“werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server can't understand.” 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>

登录.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)

1 个答案:

答案 0 :(得分: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 的情况下没有意义。