【问题标题】:How to use Delete method in python flask如何在 python 烧瓶中使用 Delete 方法
【发布时间】:2017-02-06 11:41:07
【问题描述】:

如何在 python flask 中使用 Delete 方法。我的代码段有一些问题,它在加载 url http://localhost:8082/api/users/remove_group/5 时显示 Method Not Allowed。

@app.route('/api/users/remove_group/<int:groupId>',methods=['DELETE'])
def removeGroup(groupId):
try:
    print 'its working--Delete group'
    if userGroup.query.filter_by(group_id=groupId).first() is not None:
        userGroup.query.filter_by(group_id=groupId).delete()
        message='Group removed succesfully\n'
    else:
        print 'Group not found'
        message='Group not found\n'
except HTTPException as error:
    return error(os.version)
return message

【问题讨论】:

  • 您的 html 是否指向正确的方法? stackoverflow.com/questions/24088054/…stackoverflow.com/questions/12179593/…。顺便说一句,为什么要使用 Java 标记?
  • 感谢@doctorlove 的努力,但该链接仅表示 get 和 post 方法。Delete 方法怎么样...?
  • 是的,但是错误是一样的
  • 试过methods=['GET', 'DELETE'] ?
  • 已经用 ['GET', 'DELETE'] 试过了,这很成功。但是当我只使用删除方法时,它显示方法不允许

标签: python html flask-sqlalchemy


【解决方案1】:

HTML 仅支持表单中的 GET/POST 请求,不是吗?因此,您可能会尝试使用在@app.route 中不允许的POST 请求来访问您的removeGroup 方法。

请参阅: Should PUT and DELETE be used in forms?

【讨论】:

  • 您的回答与问题无关。您的服务器如何处理 DELETE 请求。它是烧瓶,服务器端代码。请求不必来自表单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-22
  • 2013-07-27
  • 2012-06-16
相关资源
最近更新 更多