我刚开始学习 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/