[英]Flask - POST Error 405 Method Not Allowed
我刚刚开始学习 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时呈现它)工作正常。 但是,当我单击提交按钮时,出现POST 405 error Method Not Allowed
。
为什么不显示“你好” ?
您的表单正在提交到/
当为/template
路由方法时,除非这是一个错字,您应该调整表单的action
属性以指向template
视图: action="{{ url_for('template') }}"
代替:
<form action="/" method="post">
和:
<form action="{{ url_for('template') }}" method="post">
如果省略action
属性,表单将发布到当前 URL。
代替:
<form action="/" method="post">
和:
<form method="post">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.