python - Flask - 不允许使用 POST 错误 405 方法

标签 python http post flask

我刚开始学习 Flask,我正在尝试创建一个允许 POST 方法的表单。

这是我的方法:

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

还有我的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>

加载表单(在收到 GET 时呈现它)工作正常。但是,当我单击 submit 按钮时,我收到 POST 405 error Method Not Allowed

为什么不显示“Hello”

最佳答案

当方法被路由到 /template 时,您的表单正在提交到 /,除非这是一个错字,您应该调整表单的 action指向 template View 的属性:action="{{ url_for('template') }}"

关于python - Flask - 不允许使用 POST 错误 405 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12179593/

相关文章:

http - 阻止在反向代理模式下通过 http 端口访问 rocket.chat

web-services - REST API - 单个资源的更新会更改多个其他资源

swift - 在 Swift 中通过多个重定向自动从 HTTP Post 请求更新 Cookie

javascript - 在php中捕获js var

python - 与 Python Regex 完全匹配的数字或数字限制

python - __contains__ 函数的多个参数

python qcut方法对分数进行分箱

python - 使用 plt.savefig 保存图像,但图像都是白色的

angular - 403 仅在我的机器上 - Angular 2 和 Tomcat 8.5

json - 如何使用 TypeScript 在 Angular 2 中将对象正确转换为 JSON