Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask not detecting POST request from form

I'd like a POST request to be sent to my main.py file when the Login button is clicked. However, as the code currently is, the python file doesn't detect that a POST request has been sent from the login.html file's form. When the button is clicked, nothing happens. I've set a print("post") line in the case of a POST request being sent to /login but it prints nothing (I've checked my print function is working and printing properly)

Here's my part of my login.html file:
(I don't believe any of the rest is necessary to show but if it is, let me know)

    <div class="container">
        <form method="post">
            <input placeholder="Username" type="text" class="validate" value="{{ request.form.username }}">
            <input placeholder="Password" type="password" class="validate" value="{{ request.form.password }}">
            <a type="submit" value=submit class="btn waves-effect waves-teal">Login</a>
        </form>
    </div>

Here's part of my main.py file:
(I don't believe any of the rest is necessary to show but if it is, let me know)

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
    print( "post" )
    if ( request.form.get( 'username', None ) != None ) and ( request.form.get( 'password', None ) != None ):
        if request.form[ 'username' ] == "funky" and request.form[ 'password' ] == "passw":
            session[ 'loggedIn' ] = True
            session[ 'username' ] = request.form[ 'username' ]
            dictUser[ request.form[ 'username' ] ] = user( "blue" )
            print( "hi" )

if session.get( 'loggedIn', False ):
    return redirect( url_for( "index" ) )
else:
    return render_template( "login.html" )

Please note the "funky" and "passw" values are merely while I'm testing and have not port forwarded. I will add a proper check for whether the username and password is correct once I've fixed this issue (and set up a database, etc.)

In advance: I have checked to see if there are any other posts that may answer my question but I cannot find any. If there are, just point me to them and I apologise for wasting your time

UPDATE
Ok, have tried changing the <a type="submit" value=submit class="btn waves-effect waves-teal">Login</a> to an <input> and now the python file recognises that a POST request has been sent. However, when running request.form.get( "username", None ) It returns None

UPDATE II
Added in name=username and name=password to their respective inputs and that solved my secondary issue of not being able to access the values of request.form.get( stuff )

like image 869
Funk Pigeon Avatar asked Feb 12 '26 21:02

Funk Pigeon


1 Answers

Add action to your form. See below.

<form method="post" name="login" action="{{ url_for('login') }}">
    <input placeholder="Username" type="text" class="validate" value="{{ request.form.username }}">
    <input placeholder="Password" type="password" class="validate" value="{{ request.form.password }}">
    <a type="submit" value=submit class="btn waves-effect waves-teal">Login</a>
</form>
like image 75
sgetachew Avatar answered Feb 14 '26 09:02

sgetachew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!