Home > front end >  POST request to Flask Blueprint api not working
POST request to Flask Blueprint api not working

Time:03-09

I am trying to execute a form POST request. The request is received at the route api function but the POST condition is not executing.

Code

<form  action = "{{ url_for('data_sources_api.testfn') }}" method = "POST">
    <div >
        <div >
            <label for="url" >URL</label>
            <input type="text"  id="url" name="url" aria-describedby="emailHeurllp"
                placeholder="Enter new events top domain source url">
        </div>
        <div >
            <label for="name" >Name</label>
            <input type="text"  id="name" name="name" placeholder="Enter Organization Name">
        </div>
        <div >
            <button type="button"  value="submit" name = "submit">Submit</button>
        </div>
    </div>
</form>

Flask Code

@data_sources_api.route('/login/test', methods=["GET", "POST"])
def testfn():
    # Check if user is loggedin
    if loggedin():
        # User is loggedin, render the home page
        if request.method == 'POST':
            print("POST Request Received")
            result = request.form
        return render_template('users/data-sources.html', role=session['role'])
    return redirect(url_for('account_api.login'))

The request received is GET when i click the submit button and not POST :(

Thanks in advance

CodePudding user response:

I think the problem is with button. Replace the button with input type="submit":

<input type="submit" value="Submit">

Here is the example of Django form.

  • Related