【问题标题】:Flask mail api, not getting request variable properly烧瓶邮件api,没有正确获取请求变量
【发布时间】:2018-12-03 12:28:48
【问题描述】:

我正在尝试访问来自 HTML 表单的请求,并将其作为邮件发送,但我收到一封值为“无”的邮件,

这是我的代码:

@app.route("/about", methods=['GET', 'POST'])
def send_message():
     name = request.form.get('name')
     msg = Message(
       subject='Hello ' + str(name),
       sender='kristofferlocktolboll@gmail.com',
       recipients=
           ['kristofferlocktolboll@gmail.com'],
       html=render_template("about.html"))
    mail.send(msg)
    confirm_msg = "Your message has been sent!"
    return render_template("about.html", confirm_msg=confirm_msg)

我认为这可能是因为我将对象转换为字符串,但如果我不这样做,由于在字符串和另一个对象之间进行连接,我会得到一个错误

编辑:

这是我的 html 代码,我尝试过使用 post 和 get 作为方法,但没有任何效果。

  <form action="/send_message" method="post">
                First name: <br>
                <input type="text" name="name" size="35"><br>
                  Last name:<br>
                 <input type="text" name="lastname" size="35"><br>
                    Email-address: <br>
                 <input type="email" name="email" size="35"><br>
                 Phone-number: <br>
                 <input type="text" name="phone" size="35"><br>
                Enter your message: <br>
                 <textarea type="text" name="message" rows="7" cols="40"></textarea><br>

            </form>

编辑 2:

当我尝试显示 confirm_msg 时,它会在我进入网站时立即显示。

<p>{{confirm_msg}}</p>

【问题讨论】:

    标签: python flask


    【解决方案1】:

    首先你必须为你的表单添加 CSRF_TOKEN: <form method="post" action="/send_message"> <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" /> ... ..... </form>

    您还可以告诉我们您想在哪个页面查看&lt;p&gt;{{confirm_msg}}&lt;/p&gt; 吗?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 2017-08-24
    相关资源
    最近更新 更多