Python 烧瓶URL变量类型无?

Python 烧瓶URL变量类型无?,python,flask,Python,Flask,我试图通过URL传递一个数字,并在另一个页面上检索它。如果我试图指定变量类型,我会得到一个格式错误的URL错误,它将无法编译。如果我不指定var类型,它将运行,但变量将变为None类型。我也不能把它转换成整数。如何将其作为整数传递。。。?提前谢谢 这给了我一个格式错误的URL错误: @app.route('/iLike/<int: num>', methods=['GET','POST']) def single2(num): @app.route('/iLike/',方法=['G

我试图通过URL传递一个数字,并在另一个页面上检索它。如果我试图指定变量类型,我会得到一个格式错误的URL错误,它将无法编译。如果我不指定var类型,它将运行,但变量将变为None类型。我也不能把它转换成整数。如何将其作为整数传递。。。?提前谢谢

这给了我一个格式错误的URL错误:

@app.route('/iLike/<int: num>', methods=['GET','POST'])
def single2(num):
@app.route('/iLike/',方法=['GET','POST'])
def single2(数量):
这会运行,但会给我一个类型为none的变量,我无法使用它:

@app.route('/iLike/<num>', methods=['GET','POST'])
def single2(num):
     try:
        location = session.get('location')
        transType = session.get('transType')
        data = session.get('data')

        **num = request.args.get('num')**
@app.route('/iLike/',方法=['GET','POST'])
def single2(数量):
尝试:
location=session.get('location')
transType=session.get('transType')
data=session.get('data')
**num=request.args.get('num')**

在第二个示例中,不要使用
num=request.args.get('num')
尝试简单地使用
num
。由于您将其指定为路由/功能的输入,因此您应该能够直接访问它

试试这个:

@app.route('/iLike/<int:num>', methods=['GET','POST'])
def single2(num):
  print(num)
@app.route('/iLike/',方法=['GET','POST'])
def single2(数量):
打印(个)

此处混合了路由参数和请求参数

在管线中指定的参数是管线参数,是一种声明方式。这些参数的值作为函数参数传递给route函数。因此,在您的示例中,对于
url部分,该值作为函数参数
num
传递

请求参数独立于路由,并作为GET参数传递给URL。您可以通过。这就是您使用
request.args.get()
所做的

完整示例如下所示:

@app.route('/iLike/<int:num>')
def single2(num):
    print(num, request.args.get('num'))
@app.route('/iLike/'))
def single2(数量):
打印(num,request.args.get('num'))

打开
/iLike/123
现在将导致
123无
。请求参数为空,因为您没有指定参数。您可以通过打开
/iLike/123?num=456
来实现这一点,这将导致
123 456
您在此处收到
None

num = request.args.get('num')
因为您没有将
num
作为querystring的元素传递

当使用
request.args.get('num')

如果我们有这样的URL:

localhost:8080/iLike?num=2
但这不是你的情况。您已经将
num
作为参数传递给函数。因此,在您的情况下,只需使用:

@app.route('/iLike/<num>', methods=['GET','POST'])
def single2(num):
     try:
        location = session.get('location')
        transType = session.get('transType')
        data = session.get('data')

        print(num)
@app.route('/iLike/',方法=['GET','POST'])
def single2(数量):
尝试:
location=session.get('location')
transType=session.get('transType')
data=session.get('data')
打印(个)

除其他问题外,“错误URL”错误的直接原因是URL中包含的空间:

'/iLike/<int: num>'
“/iLike/”
与此相反:

'/iLike/<int:num>'
“/iLike/”

num的值将作为函数参数自动传递给函数。您不需要获取请求参数(它也会在其他地方出现,因此您不需要获取值)。