Search code examples
pythonflaskbad-request

Why am I getting a BadRequestKeyError in Python?


I am getting this error when I'm trying to run the following code in python.

Error Message

My signup.html code:

<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>

My python code:

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

I have seen similar questions and I have made the corrections along the way but I'm still getting this error. Can someone help me understand why I'm getting this error and if I have missed anything.

Thanks for your time.


Solution

  • it missing name attribute for <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>