保持表格数据在URL与URL [英] keeping forms data in url with flask
问题描述
这段代码工作正常:
@ app.route('/',methods = ['GET'])
def index():
res ='''< form action =/ searchmethod = post>
< p>< input type =textname =queryvalue =test>< / p>
< p>< input type =submitvalue =Search>< / p>
< br />
< / form>'''
return res
@ app.route('/ search',methods = ['POST'])
def search ():
return request.form ['query']
myapp.com/search
while 我想要像 myapp.com/search?query=toto
我一定错过了一些非常基本的东西,我猜...任何提示?
$ b
您创建的HTML表单最终看起来像这样: pre> < form action =/ searchmethod = post>
< p>< input type =textname =queryvalue =test>< / p>
< p>< input type =submitvalue =Search>< / p>
< br />
< / form>
注意 method = post
部分。这会告诉浏览器在提交表单时使用POST而不是GET。当它使用POST时,数据包含在结果请求的主体中。当您使用GET时,数据将包含在URL中。因此,请删除这部分HTML,以便您的表单看起来像这样,而浏览器将发出一个GET请求。
< form action =/ search>
< p>< input type =textname =queryvalue =test>< / p>
< p>< input type =submitvalue =Search>< / p>
< br />
< / form>
请注意,如果您这样做,您还需要更改搜索视图。 / b>
@ app.route('/ search')
def search():
return request.args ['query']
请注意,我已经删除了 methods = ['POST']
部分,所以现在搜索视图将接受 GET
请求。另外,我正在使用 request.args
而不是 request.form
。查看 API文档 。
更多信息,请参阅什么时候应该使用GET或POST方法,或者搜索描述其差异的其他站点(例如本站)。
了解GET和POST之间的区别(特别是HTTP和HTML如何从浏览器到服务器发出请求)对于理解像Flask这样的Web框架是如何工作是绝对必要的。
Despite looking at URL building howtos with Flask, I couldn't figure out a way to keep form data in url.
This code works fine :
@app.route('/', methods=['GET'])
def index():
res = '''<form action="/search" method=post>
<p><input type="text" name="query" value="test"></p>
<p><input type="submit" value="Search"></p>
<br />
</form>'''
return res
@app.route('/search', methods=['POST'])
def search():
return request.form['query']
But results are displayed on myapp.com/search
while I would like something like myapp.com/search?query=toto
I must have missed something pretty basic I guess... Any hint ?
Thanks in advance
The HTML form you create ends up looking like this:
<form action="/search" method=post>
<p><input type="text" name="query" value="test"></p>
<p><input type="submit" value="Search"></p>
<br />
</form>
Note the method=post
part. This tells the browser to use POST rather than GET when submitting the form. When it uses POST, the data is included in the body of the resulting request. When you use GET, the data is included in the URL. So, remove this part of your HTML so your form looks like this instead and the browser will make a GET request.
<form action="/search">
<p><input type="text" name="query" value="test"></p>
<p><input type="submit" value="Search"></p>
<br />
</form>
Note that if you do this you'll also need to change your "search" view..
@app.route('/search')
def search():
return request.args['query']
Note here that I've removed the methods=['POST']
part, so now the search view will accept the GET
request. Also, I'm using request.args
rather than request.form
. See the API Documentation for the difference.
For more info, see the answers on When should I use GET or POST method, or search for other sites that describe the difference (such as this site).
Understanding the difference between GET and POST (and specifically how HTTP and HTML work to make requests from the browser to the server) is absolutely necessary to understand how web frameworks like Flask work.
这篇关于保持表格数据在URL与URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!