Flask mail api, not getting request varialbe properly

时间:2018-06-24 23:51:54

标签: python flask

I'm trying to access a request from an HTML form, and send it as a mail, but I get a mail with a value of "None",

here is my code:

@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)

I think it might be due to the fact, that I'm casting the object into a string, but if I don't do that, I will get an error due to making a conjunction between a String and another object

EDIT:

here is my html code, i have tried both using post and get as the method, but nothing works.

  <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>

EDIT 2:

When ever i try to display the confirm_msg it is displayed instantly, when i enter the site.

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

1 个答案:

答案 0 :(得分:0)

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

您还能告诉我们您要在哪个页面上查看<p>{{confirm_msg}}</p>吗?