I am learning to write python and am working on a web application. This has a sign up system with a set of input fields and a submit button. I have confirmed that the database is connected and I can insert data into it when I run the application. When I enter data into an input field, it returns the error:
Bad Request The browser (or proxy) sent a request that this server could not understand.
Here is the python:
from flask import Flask, request, render_template, redirect, url_for
from flask_mysqldb import MySQL;
from DB_Operations import add_text
import mysql.connector
app = Flask(__name__)
db = MySQL(app);
@app.route('/')
def index():
return render_template("login.html")
@app.route('/dashboard')
def dashboard():
return render_template('dashboard.html')
@app.route("/add_text", methods=["POST", "GET"])
def AddText():
if request.method == "POST":
username = request.form["uname"]
first_name = request.form["fname"]
last_name = request.form["lname"]
password = request.form["pwd"]
email = request.form["email"]
add_new = add_text(username, first_name, last_name, password, email)
print("success")
else:
print("error")
return render_template('login.html')
if __name__ == '__main__':
app.run(debug=True)
Second file:
import mysql.connector
#database connection
mydb = mysql.connector.connect(host="localhost",user="root",database="sdv_graphs")
mycursor = mydb.cursor()
def add_text(username,first_name, last_name, password, email):
sql = "INSERT INTO tbl_user (fname, lname, upwd, umail, uname) VALUES (%s, %s, %s, %s, %s)"
val = ("{first_name}", "{last_name}", "{password}", "{email}", "{username}")
mycursor.execute(sql, val)
mydb.commit()
return 1
and here is my HTML:
<form action="/add_text" method="POST">
<div class="login-box">
<h1 id="page-title">Login</h1>
<div class="textbox sign-up-textbox">
<i class="far fa-user"></i>
<input class="input-box" id="fname" type="text" placeholder="First Name" name="First Name" onfocus="this.placeholder = '', this.parentNode.style.color = 'white'" onblur="this.placeholder = 'First Name'">
</div>
<div class="textbox sign-up-textbox">
<i class="far fa-user"></i>
<input class="input-box" id="lname" type="text" placeholder="Last Name" name="Last Name" onfocus="this.placeholder = '', this.parentNode.style.color = 'white'" onblur="this.placeholder = 'Last Name'">
</div>
<div class="textbox">
<i class="far fa-user"></i>
<input class="input-box login" id="uname" type="text" placeholder="Username" name="Username" onfocus="this.placeholder = '', this.parentNode.style.color = 'white'" onblur="this.placeholder = 'Username'">
</div>
<div class="textbox">
<i class="fas fa-lock"></i>
<input class="input-box login" id="pwd" type="password" placeholder="Password" name="Password" onfocus="this.placeholder = '', this.parentNode.style.color = 'white'" onblur="this.placeholder = 'Password'">
</div>
<div class="textbox sign-up-textbox">
<i class="fas fa-lock"></i>
<input class="input-box" id="cpwd" type="password" placeholder="Confirm Password" name="Confirm Password" onfocus="this.placeholder = '', this.parentNode.style.color = 'white'" onblur="this.placeholder = 'Confirm Password'">
</div>
<div class="textbox sign-up-textbox">
<i class="far fa-envelope-open"></i>
<input class="input-box" id="email" type="email" placeholder="Email" name="Email" onfocus="this.placeholder = '', this.parentNode.style.color = 'white'" onblur="this.placeholder = 'Email'">
</div>
<button class="submit-btn" type="submit" onclick="validateForm()" name="submit">submit</button>
</div>
</form>
Is there a simple mistake that I am making or is there an alternative approach I can use to sending this data to my database?