GET属性的数据类型 [英] Data type of GET attributes

查看:167
本文介绍了GET属性的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读[链接] http://flask.pocoo。 org / docs / quickstart /#the-request-object [a link]

如果我有一个html,它包含一个元素为:

 < input type =textid = 2 name =box>< / input> 

我正在使用Flask应用程序在GET请求中获取此dat。从文档中我知道我可以使用
$ b $ pre $ search_word $ request.args.get('box','')

我想知道searchwork的数据类型是什么,它是一个对象。如果它是一个对象有一种方法,我可以将其转换为一个整数或字符串,因为我有一个函数,它需要一个整数,我想在那里传递的搜索字符。

  from flask import请求,Flask 
$ b $ app = Flask(__ name__)

@ app.route('/',methods = ['POST','GET'])
def home():
key = request.args.get('key','')
return'key is`%s`,type(key)is%s\\\
'%( (key))

if __name__ =='__main__':
app.run()

现在运行它。

  $ wget -qO  - 'http:// localhost :5000 /?key = value'
key是`value`,type(key)是< type'unicode'>

如果您想将其转换为整数,您绝对需要某种回退。因此,结合其他的答案,你可以做这样的事情。

  return'key is`%s`,type(key)is %s,key.isdigit()是%s\\\
'%(
key,type(key),key.isdigit())

试试这个:

  $ wget -qO  - 'http:// localhost:5000 /?key = value'
key是`value`,type(key)是< type'unicode'> ;, key.isdigit()是False
$ wget -qO - http:// localhost:5000 /?key = 123'
key是`123`,type(key)是< type'unicode'> ;, key.isdigit()是True
<然后你可以使用适当的方法来处理你的成功和失败的条件,并使用 int(value) 将转换为 int


I was reading [a link]http://flask.pocoo.org/docs/quickstart/#the-request-object [a link]

If I have an html which consists of a form which has an element as:

<input type="text" id = 2 name="box"></input>

And I am using Flask app to get this dat in a GET request. from the documentation I came to know I can use

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

I want to know what is the data type of searchwork , Is it an object. If it is an object is there a way I can convert it to an integer or string because I have a function which takes an integer and I want to pass the searchword there.

解决方案

Just build a simple app and you can get your answer.

from flask import request, Flask

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def home():
    key = request.args.get('key', '')
    return 'key is `%s`, type(key) is %s\n' % (key, type(key))

if __name__ == '__main__':
    app.run()

Now run it.

$ wget -qO - 'http://localhost:5000/?key=value'
key is `value`, type(key) is <type 'unicode'>

If you want to convert that into an integer, you definitely want some kind of fallback. So incorporating the other answer, you can do something like this

    return 'key is `%s`, type(key) is %s, key.isdigit() is %s\n' % (
        key, type(key), key.isdigit())

Trying this:

$ wget -qO - 'http://localhost:5000/?key=value'
key is `value`, type(key) is <type 'unicode'>, key.isdigit() is False
$ wget -qO - 'http://localhost:5000/?key=123'
key is `123`, type(key) is <type 'unicode'>, key.isdigit() is True

Then you can use the appropriate methods to handle your success and failure conditions, and use int(value) to cast value into an int.

这篇关于GET属性的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆