我如何使我的提交按钮在不干扰我的Python代码的情况下将用户带到第二页?

问题描述 投票:0回答:1

我目前正在使用Flask处理表单。用户在两个框中输入他们的姓名和反馈,然后我使用API​​将字符串发送到我的电话号码。目前,我正在努力做到这一点,以便在提交文本后将其带到另一个页面。我尝试了几件事,例如更改表单的“操作”并返回第二页的呈现模板。但是,这些导致我立即被发送了一个空白的SMS消息,第二页显示了404,并且该站点立即将用户带到第二页并分别发送了一个空白的SMS。抱歉,如果我的解释不清楚。

摘要-我正在尝试使用户在提交表单时被带到第二页,但是我遇到了一些问题。

任何帮助将不胜感激!

HTML-

<form method="POST" class="formone" >
    <input style="font-size:55px !important; font-family: 'Ubuntu', Helvetica, Arial, sans-serif; text-align: center; margin: 10px;" type='text' name='name' placeholder='Your name' /><br/>
    <input style="font-size:55px !important; font-family: 'Ubuntu', Helvetica, Arial, sans-serif; text-align: center; margin: 10px;" type='text' name='message' placeholder='Your message'/><br/>
    <input type='submit' id="feedbackbutton" value='submit' />
</form>

Python-

@app.route("/contact.html", methods=["GET", "POST"])
def contact():
    sender = request.form.get('name')
    message = request.form.get('message')
    client.messages.create(to="+12672747668",
                           from_="+15104661095",
                           body=f"From [{sender}]: {message}")
    return render_template('/contact.html')

html flask twilio
1个回答
0
投票
first add action = "/contact" then add csrf_token into your form tag like
<form ction = "/contact" method="POST" class="formone" >
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
</form>
then in python code change route name like

@app.route("/contact", methods=["GET", "POST"])
© www.soinside.com 2019 - 2024. All rights reserved.