-1

I'm using flask to make a login system and when I POST the form from the HTML form and I keep getting the following error: werkzeug.exceptions.HTTPException.wrap.<locals>.newcls: 400 Bad Request: KeyError: 'username'.

I've looked up the error on Google and have gotten nowhere.

My HTML code is this:

<div class="card rounded-0" id="register_form">
        <div class="card-header">
            <h3 class="mb-0">Register</h3>
        </div>
        <div class="card-body">
            <form class="form" role="form" method="POST" action="/auth/register" id="form">
                <div class="form-group">
                        <label for="email">Email</label>
                        <input type="text" class="form-control form-control-lg rounded-0" id="email" required="">
                        <div class="invalid-feedback">Enter your Email.</div>
                </div>
                <div class="form-group">
                    <label for="username">Username</label>
                    <input type="text" class="form-control form-control-lg rounded-0" id="username" required="">
                    <div class="invalid-feedback">Enter your Username.</div>
                </div>
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" class="form-control form-control-lg rounded-0" id="password" required="">
                    <div class="invalid-feedback">Enter your Password.</div>
                </div>
                <button type="submit" class="btn btn-success btn-lg float-right" id="btnLogin_regi">Register</button>
            </form>
            <p>Already have an account? </p><a class="btn btn-primary btn-lg" tabindex="-1" role="button" id="login_btn">Login</a>
        </div>
</div>

My Python code is this:

@app.route('/auth/register', methods=["POST"])
def auth_post():
    if request.method == "POST":
        try:
            q = database.query(User).filter(User.username == request.form.get("username"))
            exists = database.query(q.exists()).scalar()
            if exists:
                return render_template("auth.html", error="User with same username/email exists.")
            else:
                username = str(request.form["username"])
                password = str(request.form["password"])
                email = str(request.form["email"])
                print("Username: "+username+", Password: "+password+", Email: "+email)
                temp = User(username=username, password=password, email=email)
                database.add(temp)
                database.commit()
        except sqlalchemy.orm.exc.MultipleResultsFound:
            return "Well this shouldn't be happening but it is. Join our Discord Server and tell one of the devs that the following error occured:\n<code>sqlalchemy.orm.exc.MultipleResultsFound (register)</code>"

I expect it to push the data to the SQLite Database, and once it does that, I will add more features after that.

jackmerrill
  • 400
  • 2
  • 7
  • 21

1 Answers1

0

The issue was that I didn't add name= to the input field.

Thanks @SuperShoot.

jackmerrill
  • 400
  • 2
  • 7
  • 21