【发布时间】:2021-11-16 17:10:32
【问题描述】:
下面的代码来自“Learn Python3 the Hard Way”。我收到的是 GET 而不是 POST 请求。烧瓶调试器指出在线索引中的错误'return render_template["hello_form.html"]'- TypeError: 'function' object is not subscriptable.
这意味着它没有进入“POST”部分。表格是如何工作的?是
html文件正确吗?模板目录包含:
模板/hello_form.html
<form action="/hello" method="POST">
A Greeting: <input type="text" name="greet">
</br>
Your name: <input type="text" name="name">
</br>
<input type = "submit">
</form>
模板/index.html
<body>
{% if greeting %}
I just wanted to say
<em style="color: red; font-size: 2em;">{{ greeting }} </em>.
{% else %}
<em>Hello</em>, world!
{% endif %}
</body>
app.py
@app.route("/hello", methods=['POST','GET'])
def index():
greeting = "Hello World"
if request.method == "POST":
name = request.form['name']
greet = request.form['greet']
greeting = f"{greet}, {name}"
return render_template("index.html", greeting=greeting)
else:
return render_template["hello_form.html"]
[网址] http://localhost:5000/hello
【问题讨论】: