我有一个Flask应用程序,正在尝试从HTML表单中检索一些信息,但是,我没有任何信息,我已经尝试了一切,但似乎没有任何作用。
Routes.py
@app.route("/about", methods=['POST', 'GET'])
def about():
name = request.form.get('name')
lastname = request.form.get('lastname')
msg = Message(
subject='Hello ' + str(name),
sender='kristofferlocktolboll@gmail.com',
recipients=
['kristofferlocktolboll@gmail.com'],
html= 'hello mr ' + str(lastname))
mail.send(msg)
confirm_msg = "Your message has been sent!"
return render_template("about.html", confirm_msg=confirm_msg)
about.html:
<h1 class="mb-5"> Enter your message, and i will get back to you as soon as possible</h1>
<form action="{{ url_for('about') }}" 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>
<br>
<form>
<button type="submit" class="btn btn-outline btn-xl js-scroll-trigger" value="submit" method="POST">Let's get in touch</a>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
我希望它是一个发布请求,但是每当我从@ app.route中的方法中删除'GET'属性时,都会出现“ METHOD NOT ALLOWED”错误,这可能是由于以下事实造成的:正在使用GET重定向。
电子邮件已成功发送,因此邮件API工作正常。但这是使用值“无”发送的,其中“名字”和“姓氏”属性在哪里。
编辑: 我必须将名称和姓氏对象转换为字符串,否则,我将收到非类型错误
答案 0 :(得分:0)
创建template/my-form.html
<form method="POST">
<input name="name">
<input name="lastname">
<input type="submit">
</form>
在烧瓶应用routes.py
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template('my-form.html')
@app.route('/', methods=['POST'])
def my_form_post():
name = request.form['name']
lastname = request.form['lastname']
<do your manipulation>
return render_template(yourtemplate,processedtext = processedtext,somethingelse=somethingelse)
您可以将第二个渲染模板中的物品替换为
<html>
<body>
{{processedtext}}
{{somethingelse}}
</body>
</html>
并显示它们