在服务器上找不到请求的URL。(但为什么?)

2024-05-23 14:35:58 发布

您现在位置:Python中文网/ 问答频道 /正文

老实说,我不知道我应该问什么问题。也许有人可以编辑我的标题以便更具描述性

我有一个python flask应用程序,有时会返回:

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

这种情况何时发生是有规律的,但我不知道为什么会发生

我有一个主页,用户单击谷歌地图上的一个标记,然后单击一个超链接“查看天气预报”按钮,这会将他们带到天气预报页面: enter image description here

在我的主python文件(相当于index.py)中,我为下一页创建路由,如下所示:

@app.route("/weather_forecast/<climbing_area>/<city_id>/<lat>/<lon>/<weather_site>")    #creates url using climbing area name, city id, lat, lon, and location of weather site
def weather_forecast(climbing_area, city_id, lat, lon, weather_site):

    return render_template('weather_forecast.html', 
                       climbing_area=climbing_area, 
                       city_id=city_id, 
                       daily_forecast=wf.format_daily_forecast(city_id), 
                       extended_forecast=wf.format_extended_forecast(city_id), 
                       historical_forecast=wf.get_historical_weather(lat, lon),
                       dates=wf.get_date_range(),
                       lat=lat,
                       lon=lon,
                       weather_site=weather_site,
                       sites=db.create_site_list_sqlite(),
                       api_key=os.environ.get('GOOGLE_MAPS_JS_API_KEY'),
                       image_url=wf.image_choice(lat, lon, city_id))

如你所见,地图上有许多标记,其中95%的标记做了正确的事情,点击后进入天气预报页面。但其中有一些没有。它们总是不起作用

每个天气预报站点的URL由此模板动态生成:

/weather_forecast/<climbing_area>/<city_id>/<lat>/<lon>/<weather_site>

这些是从/主页路由返回的变量。我的第一个想法是:因为这些是从后端(SQLite)动态生成的变量,可能没有为这些特定站点返回数据,这会导致页面失败?但我已经为这些选定的几个站点验证了数据是可用的

此URL可用于:

http://localhost:5000/weather_forecast/Humboldt%20County/5558953/41.03/-124.113/Arcata

此URL不起作用:

http://localhost:5000/weather_forecast/Mount%20Yonah/Tallulah%20Gorge/4188197/34.637/-83.72200000000001/Clarkesville

我不希望有一个神奇的代码修复,但有人能建议我还可以寻找什么?或者在我的问题中包含哪些其他信息会有帮助


Tags: 标记idurlcitygetsitearea页面
3条回答

允许的URL为: /weather_forecast/<climbing_area>/<city_id>/<lat>/<lon>/<weather_site>

您可能正在尝试访问URL:

/weather_forecast/<climbing_area>/<city_id>/<lat>/<lon>/<weather_site>/

URL的末尾有一条斜线。由于flask代码中的URL结尾没有斜杠,因此会抛出错误

另外,请发布您正在点击的URL。这将有助于提供更准确的解决方案

我找到了解决办法。原来我在不知不觉中传递了一个错误的URL

此URL有效: http://localhost:5000/weather_forecast/Humboldt%20County/5558953/41.03/-124.113/Arcata

此URL不起作用: http://localhost:5000/weather_forecast/Mount%20Yonah/Tallulah%20Gorge/4188197/34.637/-83.72200000000001/Clarkesville

区别在于,在我的weather_forecast()函数中,我创建了如下路由: @app.route("/weather_forecast/<climbing_area>/<city_id>/<lat>/<lon>/<weather_site>")

我的一些网站(和URL)失败的原因是<;攀岩区>;变量在数据库记录中有一个/字符。例如,如果<climbing_area> = Mount Yonah/Tallulah Gorge,这会在我的URL中添加一个额外的/字符,导致它失败

我本可以想出如何让flask接受literal/字符。但是在本例中,我只是更改了我的<climbing_area>名称以删除/。问题解决了

我想,你需要设置正确的field和正确的精度来存储谷歌地图latlon,就像这个thread

在您的模型中,尝试以下操作:

..

    lat = db.Column(db.Numeric(22, 16), nullable=False)
    lon = db.Column(db.Numeric(22, 16), nullable=False)

# or 

    lat = db.Column(db.DECIMAL(22, 16), nullable=False)
    lon = db.Column(db.DECIMAL(22, 16), nullable=False)

..

我建议您检查一下weather_forecast()函数的代码,它返回的数据可以在weather_forecast.html模板中使用image_url等相同参数进行计算

@app.route("/weather_forecast/<climbing_area>/<city_id>/<lat>/<lon>/<weather_site>")
def weather_forecast(climbing_area, city_id, lat, lon, weather_site):

..
    sites=db.create_site_list_sqlite()

..
    return render_template('weather_forecast.html', 
                       climbing_area=climbing_area, 
                       city_id=city_id, 
                       lat=lat,
                       lon=lon,
                       weather_site=weather_site,

                       sites=sites,  # to keep things coherent 
                       
                       wf=wf)

而不是发送

                       daily_forecast=wf.format_daily_forecast(city_id), 
                       extended_forecast=wf.format_extended_forecast(city_id), 
                       historical_forecast=wf.get_historical_weather(lat, lon),
                       dates=wf.get_date_range(),
                       image_url=wf.image_choice(lat, lon, city_id)

只需发送wf对象,然后在weather_forecast.html模板中使用它,就像:

<img src="{{ wf.image_choice(lat, lon, city_id) }}" atl="">

另外,您不需要发送api_key,因为您可以直接从模板中的全局config对象获取它,如

{{ config.GOOGLE_MAPS_JS_API_KEY }}

相关问题 更多 >