My HTML is not sending the POST method to the flask app function

Question:

  1. What’s happening:

I have an HTML code that is sending a POST request to the flask code. It’s for a Login page. (I’m using SQLite3 and Flask.)

  1. The HTML code:

   <div class="col">
            <div class="login-box">
            <h2>Register</h2>
            <form method="post" action="{{ url_for('register_post') }}">
                <div class="user-box">
                    <input type="email" name="email" placeholder="Email Address" required>
                </div>
                <div class="user-box">
                    <input type="password" name="password" placeholder="Password" required>
                </div>
                <div class="user-box">
                    <input type="text" name="username" placeholder="Username" required>
                </div>
                <div class="button-form">        
                    <a id="submit" href="{{url_for('register_post')}}">Submit</a>
                    <div id="register">
                        Already Have An Account ?
                        <a href="{{url_for('login')}}">Login Now !</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
  1. The Python code:

@app.route('/register', methods=['POST'])
def register_post():
    print("Got a data")
    username = request.form['username']
    email = request.form['email']
    password = request.form['password']
    c.execute('''INSERT INTO users (email, username, password) VALUES (?,?,?)''', (email, username, password))
    conn.commit()
    conn.close()

    return redirect(url_for('login'))

There isn’t a traceback which (maybe) means the function hasn’t even been called.

Asked By: pydal1

||

Answers:

I have not worked with Flask, but have with Django.

I see you got an <a> tag with href="{{url_for('register_post')}}", why are you trying to redirect to ‘https://example.com/register’? By redirecting you are making GET request whereas your register_post function only handles POST method.

You described <form> with method="post" and action="{{url_for('register_post')}}", this means that the function will work whenever someone submits the form, but for now it’s possible when you click the ‘enter’ key when focused on any input of the form. Another way to do it is to add <button type="submit">yout text</button> or <input type="submit" value="your text" /> instead of <a id="submit"> tag.

There is only two methods of HTTP Request in HTML those being POST and GET.

  • GET request occurs when you are navigating via URL or submit a form with method="GET" which is default and you do not have to specify it.
  • POST method occurs when someone SUBMITS a FORM with POST method described.

If you want to access any other HTTP Requests kind of PUT, DELETE, HEAD, OPTIONS, PATCH, then you need JavaScript for it.

Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.