为什么在Python中收到BadRequestKeyError?

时间:2018-12-06 16:28:20

标签: python flask bad-request

尝试在python中运行以下代码时遇到此错误。

Error Message

我的signup.html代码:

<html>

<head>
  <title>Signup</title>
  <link href="../static/styles/bookreview.css" rel="stylesheet" 
  type="text/css">
</head>

<body>

<!--Specifying navigation bar-->
<nav>
    <img id="logo" src="../static/images/OUAT Logo.png" alt="Once Upon A Time Logo">
    <ul>
        <li><a href="/">Home</a></li>
        <li><a href="Review Books">Books</a></li>
        <li><a href="Guestbook">Guestbook</a></li>
        <li><a href="Contact Us">Contact</a></li>
    </ul>
    <img id="searchicon" src="../static/images/Search Icon.png">
    <form>
        <input id=searchbar type=text>
    </form>
    <a href="Login"><button id=navsignin>Sign in</button></a>
</nav>

<div class="containers signupcontainer">
    <img id=signupgraphic src="../static/images/signupgraphic.png">
    <h2>Create an account and start <font color="#FF6572">commenting!</font>
    </h2>
    <!--{{msg}}-->
    <form action="" method="POST">
        <label>Full Name</label>
        <input class=signupfields type="text">

        <label>E-mail address</label>
        <input class=signupfields type="text">

        <label>Enter new password</label>
        <input class=signupfields type="password">

        <label>Re-enter password</label>
        <input class=signupfields type="password">

        <button class="buttons signupbutton">Create account</button>
    </form>
</div>
</body>

</html>

我的python代码:

from flask import Flask, redirect, request, url_for, render_template
app = Flask(__name__, static_folder='static')
import sqlite3 as s
DB_FILE = 'mydb'
connection = s.connect(DB_FILE, check_same_thread=False)

@app.route('/')
def index():
   return render_template('Index.html')


@app.route('/Review Books')
def review():
   return render_template('Review.html')

@app.route('/Guestbook')
def guestbook():
   return render_template('Guestbook.html')


@app.route('/Contact Us')
def contact():
   return render_template('Contact.html')

@app.route('/Login')
def login():
   return render_template('Login.html')

def _insertuser(name, email, password, repeatpass):
   params = {'name': name, 'email': email, 'password': password, 
  'repeatpass': repeatpass}
   cursor = connection.cursor()
   cursor.execute("insert into users(name, email, password, repeatpass) 
   values (:name, :email, :password, :repeatpass)", params)
   connection.commit()

@app.route('/Signup', methods=['POST', 'GET'])
   def signup():
      if request.method == 'POST':
         _insertuser(request.form['name'], request.form['email'], 
         request.form['password'], request.form['repeatpass'])
         return render_template('Signup.html', msg=" thank you for signing 
         up")
      else:
         return render_template('Signup.html')

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

My signup form

我看到了类似的问题,并且在此过程中进行了更正,但是仍然出现此错误。有人可以帮助我了解为什么我会收到此错误以及是否错过了任何内容。

感谢您的宝贵时间。

2 个答案:

答案 0 :(得分:0)

我认为您得到BadRequestKeyError是因为在您的Signup路由中,当您发布数据时,request.form词典中没有任何价值。

如果您编辑问题并在Signup.html中向我们展示您的表格,也许我可以找到更多。您可能没有为某些输入或某些输入赋予适当的name属性。

答案 1 :(得分:0)

它缺少name的{​​{1}}属性

<input>