烧瓶werkzeug.exceptions.BadRequestKeyError [英] Flask werkzeug.exceptions.BadRequestKeyError

查看:123
本文介绍了烧瓶werkzeug.exceptions.BadRequestKeyError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了几种不同的flask应用程序,每次都遇到以下错误.

I've tried several different flask apps and get the following error every time.

werkzeug.exceptions.BadRequestKeyError

werkzeug.exceptions.BadRequestKeyError

werkzeug.exceptions.HTTPException.wrap..newcls:400错误请求:KeyError:'名称'

werkzeug.exceptions.HTTPException.wrap..newcls: 400 Bad Request: KeyError: 'name'

我不知道为什么.我的安装程序一定有问题,因为在不同的应用程序中会发生相同的事情.我建立了一个非常简单的模型,用最少的代码来演示.如果删除 name 键,则使用 number1 键也会出现相同的错误.而且我什至找不到任何关于此错误的含义.

I can't figure out why. It must be something with my setup because the same thing is happening in different applications. I set up a very simple model with minimal code to demonstrate. If I remove the name key I get the same error with the number1 key. And I can't find anything on what this error even means.

app.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///flow.sqlite3'

db = SQLAlchemy(app)

from routes import *

if __name__ == '__main__':
    # db.drop_all()
    db.create_all()
    app.run()

routes.py

routes.py

from flask import render_template, request
from app import app, db
from models import Info

@app.route('/', methods=['GET', 'POST'])
def index():
    data = Info(request.form['name'], request.form['number1'], request.form['number2'])
    db.session.add(data)
    db.session.commit()
    return render_template('index.html', data=data)

models.py

models.py

from app import db

class Info(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    number1 = db.Column(db.Integer)
    number2 = db.Column(db.Integer)

    def __init__(self, name, number1, number2):
        self.name = name
        self.number1 = number1
        self.number2 = number2

index.html

index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <form id='basic' action="index.html" method="post">
      <label for="name">Name:</label>
      <input type="text" name="name" placeholder="Name">
      <label for="number1">Number 1:</label>
      <input type="number" name="number1">
      <label for="number2">Number 2:</label>
      <input type="number" name="number2">
      <button id='btn_submit' type="submit" name="button">Submit</button>
    </form>
  </body>
</html>

推荐答案

首次加载页面时,没有 request.form 字典,这就是为什么会出现关键错误的原因.您需要编写一条if语句来检查表单是否已提交或页面是否首次加载.

When your page is first loaded, there is no request.form dictionary, which is why you get the key error. You need to write an if statement to check if the form was submitted or if the page is being loaded the first time.

from flask import redirect, url_for

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST': 
      data = Info(request.form['name'], request.form['number1'], request.form['number2'])
      db.session.add(data)
      db.session.commit()
      return redirect(url_for('index'))
    else:
      return render_template('index.html')

这篇关于烧瓶werkzeug.exceptions.BadRequestKeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆