使用URL中的浮点数给出404的Flask路由 [英] Flask route giving 404 with floating point numbers in the URL

查看:187
本文介绍了使用URL中的浮点数给出404的Flask路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Flask应用程序的server.py中有以下路径定义:

  @ app.route('/ nearby / < float:lat> /< float:long>')
def db.trucks.find({'loc':{'$ near' :[lat,long]}})。limit(5):
如果truck.has_key('loc'):
del卡车['loc']
返回json.dumps(卡车)

但是当我到 http:// localhost:5000 / nearby /37.7909470419234/-122.398633589404 ,我得到一个404。



其他路线工作正常,所以这是一个问题。我在这里做错了什么?

解决方案

由于内置的​​FloatConverter只能处理正数,我将坐标作为字符串传递,并使用Python的float()方法将它们转换为浮点数。

I have the following route definition in my Flask app's server.py:

@app.route('/nearby/<float:lat>/<float:long>')
def nearby(lat, long):
    for truck in db.trucks.find({'loc': {'$near': [lat, long]}}).limit(5):
        if truck.has_key('loc'):
            del truck['loc']
    return json.dumps(trucks)

But when I go to http://localhost:5000/nearby/37.7909470419234/-122.398633589404, I get a 404.

The other routes work fine, so it's an issue with this one. What am I doing wrong here?

解决方案

Since the built in FloatConverter can only handle positive numbers, I pass the coordinates as strings, and use Python's float() method to convert them to floats.

这篇关于使用URL中的浮点数给出404的Flask路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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