Search code examples
pythonflaskflask-sqlalchemy

submit button not posting


When the form is submitted, it is not posted to the view function. Instead, it just changes the URL of the current webpage (https://i.sstatic.net/HXwIQ.png).

<form class="form-inline my-2 my-lg-0">
    <input class="form-control mr-sm-2" type="search" placeholder="Search" id="query"     name="query"/>
    <button class="btn btn-outline-primary my-2 my-sm-0" type="submit"><i class="bi bi-search">      </i>
    </button>
</form>


@auth.route('/search', methods=['GET', 'POST'])
def search():
    if request.method == 'POST':
        query_name = request.form.get('query')
        results = Edetails.query.filter(Edetails.name.icontains(query_name)).all()
        return render_template('search.html', results=results, user=current_user)
    
    return render_template("search.html", user=current_user)

Solution

  • Your form is missing the method attribute, which will cause a GET request. If you want to make a POST request you will have to add the method attribute and set it to post

    <form method="POST" class="form-inline my-2 my-lg-0">