简体   繁体   English

在烧瓶应用程序中使用GET请求而不是POST传递数据

[英]Passing data with GET request instead of POST in flask app

I am building flask app and I am trying to understand the routes , methods in flask documentation. 我正在构建烧瓶应用程序,我正在尝试了解烧瓶文档中的路线和方法。 I wrote a code which uses GET to submit the fields of a form : 我编写了一个使用GET提交表单字段的代码:

@app.route('/',endpoint='buf')
def index():
    page = """
    <DOCTYPE! html>
    <html lang="en-US">
    <head>
    <meta charset=utf-8">
    </head>
    <body>
    <form action="/hello" method="GET">
    First name: <input type="text" name="fname" id="fname" ><br>
    Last name: <input type="text" name="lname"><br>
    <input type="submit" value="Submit">
    </form>
    </body>
    </html>
    """   
    return page


@app.route('/hello',endpoint="new",methods=['GET','POST'])
def index():
        if request.method=='POST':
                return 'Hello %s' % (request.form['fname'])
        else:
                return 'Hello %s' % (request.form['fname'])

I get an error when I use 'GET' instead of 'POST' in my html form tag. 当我在html表单标签中使用'GET'而不是'POST'时出现错误。 Is there a way I can access the fields of the form using GET instead of POST? 有没有办法可以使用GET而不是POST来访问表单的字段?

From the relevant section at the quickstart guide 快速入门指南相关部分

To access parameters submitted in the URL (?key=value) you can use the args attribute: 要访问URL中提交的参数(?key = value),您可以使用args属性:

searchword = request.args.get('key', '')

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

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