在Flask中使用url_for发出POST请求 [英] 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.
我尝试按如下所示指定_method
,但是它仍然使用GET而不是POST传递参数.
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>
(无论我是否specify _method
,错误消息都是相同的.)
(The error message is the same whether or not I specify _method
).
推荐答案
所有链接都是GET
请求.您不能强制POST
.
All links are GET
requests. You can't force a POST
.
替代方法是:
@app.route('/save_info/<filepath>', methods=['GET', 'POST'])
def save_info(filepath):
if request.method == 'POST' or filepath:
...
您将必须找到一种很好的方法来强制您的代码忽略您发送的GET
请求.
You'll have to find a nice way to force your code to ignore that you sent a GET
request.
这篇关于在Flask中使用url_for发出POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!