在Flask中使用url_for发出POST请求 [英] Issue a POST request with url_for in Flask

查看:812
本文介绍了在Flask中使用url_for发出POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Jinja 模板中发出POST请求://flask.pocoo.org/"rel =" nofollow noreferrer>烧瓶.但是,默认情况下,参数是通过GET传递的,并且该特定方法仅接受POST请求.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆