Flask Python submit button

14,470

Your input fields have no name attribute. This will cause all of your checks to result in KeyErrors. The first step is to add the attribute to each input.

<input name="username" type="text" id="inputUsername" class="form-control" value="{{ request.form.username }}" placeholder="Username" required autofocus>

Note that I also checked the type attribute as there is no username type. email and password are valid values, email being added in HTML5.

The next step will be to change how you check for the fields. If you only care about the presence of the field, in is the way to go.

if 'username' not in request.form:

If, however, you also want a truty value, the get method is what you want.

if not request.form.get('username'):
Share:
14,470
alpi
Author by

alpi

Updated on June 04, 2022

Comments

  • alpi
    alpi almost 2 years

    I am trying to create a page to register users but the submit button in my bootstrap form isn't working. When I hit the submit button, I get a bad request error. Here is the code in my python file:

    @app.route('/register', methods=['GET', 'POST'])
    def register():
        if request.method == 'POST':
            if not request.form['username']:
                error = 'You have to enter a username'
            elif not request.form['email'] or '@' not in request.form['email']:
                error = 'You have to enter a valid email address'
            elif not request.form['password']:
                error = 'You have to enter a password'
            elif get_user_id(request.form['username']) is not None:
                error = 'The username is already taken'
            else:
                print(request.form['username'])
                db = get_db()
                db.execute('INSERT INTO user (username, email, pw_hash) VALUES (?, ?, ?)',
                           [request.form['username'], request.form['email'],
                            generate_password_hash(request.form['password'])])
                db.commit()
                flash('You were successfully registered and can login now')
                return render_template('control.html')
        return render_template('register.html')
    

    also i have a html file register.html:

    {% extends 'layout.html' %}
    {% block title %}Sign-up{% endblock title %}
    {% block body %}
    <div class="container">
        <form class="form-register" role="form" method="post" action="{{ url_for('register') }}">
            <h2 class="form-register-heading">Please sign up</h2>
            <label for="username" class="sr-only">Username</label>
            <input type="username" id="inputUsername" class="form-control" value="{{ request.form.username }}" placeholder="Username" required autofocus>
            <label for="email" class="sr-only">Email address</label>
            <input type="email" id="inputEmail" class="form-control" value="{{ request.form.email }}" placeholder="Email address" required autofocus>
            <label for="password" class="sr-only">Password</label>
            <input type="password" id="inputPassword" class="form-control" placeholder="Password" required >
            <button class="btn btn-lg btn-primary btn-block" type="submit">Sign up</button>
        </form>
    
    </div>
    {% endblock body %}
    

    I can't find where I did it wrong, I'm new to python and flask!

  • alpi
    alpi over 9 years
    Thank you so much buddy, first step of your answer fixed my problem completely!