[python] Flask Error: "Method Not Allowed The method is not allowed for the requested URL"

I'm getting the following error whenever I try to submit data to my Flask form:

Method Not Allowed The method is not allowed for the requested URL.

I think the issue is in the return redirect(url_for('database')) I'm doing. I've also tried return render_template('database.html) too. I'm trying to call the database page once form entries have been submitted to the database.

Relevant parts of my code are as follows:

@app.route('/entry', methods=['GET', 'POST'])
def entry_page():
    if request.method == 'POST':
        date = request.form['date']
        title = request.form['blog_title']
        post = request.form['blog_main']
        post_entry = models.BlogPost(date = date, title = title, post = post)
        db.session.add(post_entry)
        db.session.commit()
        return redirect(url_for('database'))
    else:
        return render_template('entry.html')

@app.route('/database')        
def database():
    query = []
    for i in session.query(models.BlogPost):
        query.append((i.title, i.post, i.date))
    return render_template('database.html', query = query)

entry.html is...

THIS IS THE BLOG ENTRY PAGE

blog:
<html>
    <form action='/database' method = "post">
        date<input name = "date" type = "text" class="text">
        title<input name = "blog_title" type = "text" class="text">
        main<input name = "blog_main" type = "text" class="text">
        <input type = "submit">
    </form> 
</html>

and database.html...

THIS IS THE QUERY:

{{query}}

This question is related to python flask

The answer is


I had a similar problem when I deployed my Flask app in the IIS. Apparently, IIS does not accept route that include an underline ("_"). When I removed the underline, problem was resolved.


I had the same problem, and my solving was to replace :

return redirect(url_for('index'))

with

return render_template('indexo.html',data=Todos.query.all())

in my POST and DELETE route.


I also had similar problem where redirects were giving 404 or 405 randomly on my development server. It was an issue with gunicorn instances.

Turns out that I had not properly shut down the gunicorn instance before starting a new one for testing. Somehow both of the processes were running simultaneously, listening to the same port 8080 and interfering with each other. Strangely enough they continued running in background after I had killed all my terminals. Had to kill them manually using fuser -k 8080/tcp