Python 发送POST和GET到同一函数时出错,

Python 发送POST和GET到同一函数时出错,,python,post,flask,jinja2,werkzeug,Python,Post,Flask,Jinja2,Werkzeug,这是一个函数,它(在GET请求中)接收case_url和case_键,并将相应的case(使用mongoDB)提供给名为detail_case的html模板。 我正在尝试添加一个功能,当填写表单(在同一页的detail_案例中)并提交表单时,它应该向同一个函数提交一个POST请求,并且应该执行“if request.method==”POST“下的代码 @app.route('/case/<case_url>/<case_key>', methods=["GET","PO

这是一个函数,它(在GET请求中)接收case_url和case_键,并将相应的case(使用mongoDB)提供给名为detail_case的html模板。 我正在尝试添加一个功能,当填写表单(在同一页的detail_案例中)并提交表单时,它应该向同一个函数提交一个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')
@app.route('/case/',methods=[“GET”,“POST”])
def SERVER_案例(案例url、案例密钥):
“”“用于在数据库中保存注释。注释”“”
如果request.method==“POST”:
text=request.form['comment\u text']
#将其插入数据库的代码
返回重定向(url_for('SERVICE_case')\
案例url=案例url\
案例(重点)
"""
函数根据URL中指示的键提供案例
"""
#此处对应的代码,用于填充变量值并将其发送到另一个页面
返回渲染模板('detail\u case.html')
问题是我认为POST请求永远不会执行。这是模板页面detail_案例中的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_文本发送到函数。事实上,POST下的代码在我提交时不会执行。 基本上,问题是在GET请求期间,它发送函数SERVICE_case的参数中所需的两个变量。在我的POST请求中,我不知道如何准确地设置动作字段。如果我没有发送参数,那就是一个错误。如果我不将它发送到同一个函数,那么它将如何执行POST代码?有人能给我提个建议吗?
我对flask很陌生,我正在编辑其他人的代码

您需要提交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>



您需要提交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>



使用Firebug或Chrome开发工具等工具查看您点击的url是什么?使用Firebug或Chrome开发工具等工具查看您点击的url是什么?哦,是的,当然,我根本没有提交它。我真傻。非常感谢。哦,是的,当然,我根本没有提交。我真傻。非常感谢你。