Categories
Python Answers

How to fix POST Error 405 Method Not Allowed with Flask Python?

Spread the love

Sometimes, we want to fix POST Error 405 Method Not Allowed with Flask Python.

in this article, we’ll look at how to fix POST Error 405 Method Not Allowed with Flask Python.

How to fix POST Error 405 Method Not Allowed with Flask Python?

To fix POST Error 405 Method Not Allowed with Flask Python, we should make sure the action attribute of the form is set to the URL of the view that accepts POST requests.

For instance write

@app.route('/template', methods=['GET', 'POST'])
def template():
    if request.method == 'POST':
        return "Hello"
    return render_template('index.html')

to create the template view.

Then in index.html, we write

<form action="{{ url_for('template') }}" method="post">
  ...
</form>

to add a form that has the action attribute set to the URL for the template view that we get with url_for('template').

Then when we submit the form, the template view will be run since we have 'POST' in the methods list.

Conclusion

To fix POST Error 405 Method Not Allowed with Flask Python, we should make sure the action attribute of the form is set to the URL of the view that accepts POST requests.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *