Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue a POST request with url_for in Flask

I'm trying to issue a POST request within a Jinja template in Flask. However, parameters are passed in via GET by default, and this particular method only accepts POST requests.

I tried specifying _method, as below, but it still passes the parameter with GET instead of POST.

<li><a href = "{{ url_for('save_info', _method='POST', filepath=s.name ) }}"><div class="nodes">{{ s.title}} - {{ song.owner }}</div></a></li>

(The error message is the same whether or not I specify _method).

like image 743
chimeracoder Avatar asked Feb 12 '12 06:02

chimeracoder


People also ask

How does url_for work in Flask?

Flask url_for is defined as a function that enables developers to build and generate URLs on a Flask application. As a best practice, it is the url_for function that is required to be used, as hard coding the URL in templates and view function of the Flask application tends to utilize more time during modification.

What is the use of url_for?

The url_for() function is very useful for dynamically building a URL for a specific function. The function accepts the name of a function as first argument, and one or more keyword arguments, each corresponding to the variable part of URL.

What is get and POST in Flask?

A GET message is send, and the server returns data. POST. Used to send HTML form data to the server. The data received by the POST method is not cached by the server.


1 Answers

You can add a Middelway which search for a GET argument which overwrite the http method. Look there: http://flask.pocoo.org/snippets/38/

Your new link will look like this:

<li><a href = "{{ url_for('save_info', __METHOD_OVERRIDE__='POST', filepath=s.name ) }}"><div class="nodes">{{ s.title}} - {{ song.owner }}</div></a></li>
like image 198
Jarus Avatar answered Sep 25 '22 23:09

Jarus