首页 > 解决方案 > 为什么我在 Python 中收到 BadRequestKeyError?

问题描述

当我尝试在 python 中运行以下代码时出现此错误。

错误信息

我的 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)

我的注册表单

我已经看到了类似的问题,并且我一直在进行更正,但我仍然收到此错误。有人可以帮我理解为什么我会收到这个错误,以及我是否遗漏了什么。

谢谢你的时间。

标签: pythonflaskbad-request

解决方案


它缺少name属性<input>

<form action="" method="POST">
    <label>Full Name</label>
    <input name="name" class=signupfields type="text">

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

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

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

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

推荐阅读