Flask - POST错误405方法不允许 [英] Flask - POST Error 405 Method Not Allowed

查看:2486
本文介绍了Flask - POST错误405方法不允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Flask,并试图创建一个允许POST方法的表单。这里是我的方法:

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

和我的index.html:

 < HTML> 
< head>
< title>标题< / title>
< / head>
< body>
输入Python执行:
< form action =/method =post>
< input type =textname =expression/>
< input type =submitvalue =Execute/>
< / form>
< / body>
< / html>

加载表单(在接收到GET的时候呈现)工作正常。当我点击提交按钮但是,我得到一个POST 405错误方法不允许。为什么不显示Hello?

解决方案

您的表单正在提交给 / / template ,除非这是一个错字,你应该调整你的表单的 action 属性和指向'/ template'


I'm just starting to learn Flask, and I am trying to create a form which will allow a POST method. Here's my method:

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

And my index.html:

<html>
  <head>
    <title> Title </title>
  </head>
  <body>
    Enter Python to execute:
    <form action="/" method="post">
      <input type="text" name="expression" />
      <input type="submit" value="Execute" />
    </form>
  </body>
</html>

Loading the form (rendering it when it receives GET) works fine. When I click on the submit button however, I get a POST 405 error Method Not Allowed. Why isn't it displayed Hello?

解决方案

Your form is submitting to / when the method is routed for /template unless that is a typo, you should adjust your form's action attribute and point it to '/template'.

这篇关于Flask - POST错误405方法不允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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