简体   繁体   English

Flask 邮件 API,未正确获取请求变量

[英]Flask mail api, not getting request variable properly

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",我正在尝试从 HTML 表单访问请求,并将其作为邮件发送,但我收到一封值为“无”的邮件,

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.这是我的 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>

EDIT 2:编辑2:

When ever I try to display the confirm_msg it is displayed instantly, when I enter the site.当我尝试显示 confirm_msg 时,它会在我进入网站时立即显示。

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

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

Also can you tell us in which page you are trying to see <p>{{confirm_msg}}</p> ?另外,您能告诉我们您要在哪个页面查看<p>{{confirm_msg}}</p>吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM