1

这是一个函数,它(在 GET 请求中)接收 case_url 和 case_key 并将相应的案例(使用 mongoDB)提供给名为 detail_case 的 html 模板。我试图添加一个功能,当填写表单(在同一页面上 detail_case)并提交时,它应该向相同的函数提交 POST 请求,并且'if request.method = =“POST”'下的代码应该被执行。

@app.route('/case/<case_url>/<case_key>', methods=["GET","POST"])
def serve_case(case_url,case_key):
"""for saving a comment in db.comments"""
if request.method == "POST":    

    text=request.form['comment_text']
    #code which inserts it in the database

    return redirect(url_for('serve_case', \
    case_url=case_url,\
    case_key="Highlights"))

"""
Function serves the case as per the key indicated in the URL
"""

#corresponding code here which fills values of variables and sends it to another page

return render_template('detail_case.html')

问题是我认为 POST 请求从未被执行过。这是模板页面detail_case上的html代码-

<textarea placeholder="Please enter your comments here" action="{{ url_for('serve_case',case_url=case_url,case_key=case_key)}}" method="POST" name="comment_text" rows="6"></textarea><br />

我认为问题是行动领域。我不知道应该如何将变量 comment_text 发送到我的函数。事实上,当我提交时,POST 下的代码并没有被执行。基本上问题在于,在 GET 请求期间,它发送了函数 serve_case 的参数中需要的 2 个变量。在我的 POST 请求期间,我不知道如何准确地构建操作字段。如果我不发送任何参数,则它是一个错误。如果我不将它发送到同一个函数,那么它将如何执行 POST 代码?有人可以建议 sumthing 吗? 我对烧瓶很陌生,我正在编辑别人的代码

4

1 回答 1

4

您需要提交 POST 请求(例如通过表单),如下所示:

<form action="{{ url_for('serve_case',case_url=case_url,case_key=case_key)}}" method="POST">
 <input type="text" placeholder="Please enter your comments here">
 <input type="submit"   name="comment_text" rows="6"><br />
</form>
于 2013-08-22T10:55:45.767 回答