[英]How to get the data in flask from the HTML input with changing the url of website
我有一个HTML输入框,我想要的是在单击按钮时获取在该框中输入的数据。 我知道如何从表单中获取数据
ex = request.form['example'];
我想像上面一样得到烧瓶中的数据,但对于看起来像这样的输入框 -
<div class="form-inline" id="inputDiv">
<center>
<input class="form-group" id="msg-content" autocomplete="off" placeholder="Send a message" />
<button class="btn btn-lg" onclick="sendMsg()" id="sendBtn"><span class="glyphicon glyphicon-send"></span></button>
</center>
</div>
创建您的路线
@app.route('/yayform',methods = ['POST', 'GET'])
def yayform():
if request.method == 'POST':
result = request.form
return render_template("result.html",result = result)
确保将表单操作添加到HTML中,
<form action="http://localhost:5000/yayform" method="POST">
如果您希望表单返回数据,通常需要将其放在<form>
元素中。
这告诉浏览器,当表单由属性type="submit"
的按钮提交时,它应该返回内部元素。 您应该输入输入属性name="example"
以便在flask中检索它。 我不确定你用sendMsg()函数调用了什么javascript,但我猜测<form>
元素是你最好的解决方案。 像这样的东西:
<form action="/url/to/send/form" method="POST">
<center>
<input class="form-group" name="example" id="msg-content" autocomplete="off" placeholder="Send a message" />
<button class="btn btn-lg" type="submit" id="sendBtn"><span class="glyphicon glyphicon-send"></span></button>
</center>
</form>
然后你应该能够按照你在帖子中描述的那样检索烧瓶。
您需要将HTML包装在表单中,例如:
<form id="inputDiv" method="post" action="/your_action">
<!--- Your HTML input and button here --->
</form>
注意method="POST"
和action="/your_action"
。
method="POST"
表示您正在向新页面发起HTTP Post请求。
action="/your_action"
是您要重定向到的网址。 您必须将其替换为您的网址。
要触及上一个答案,我不建议在操作中将"http://localhost:5000"
设置为1.这是不必要的2.它可能不正确,具体取决于您运行应用程序的方式,例如,如果你在一个不是5000的端口上运行。
在后面的python代码中:
@app.route('/your_action', methods = ['POST'])
def yourRoute():
#Your code here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.