Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python Flask-在服务器上找不到请求的URL。(但为什么?)_Python_Flask - Fatal编程技术网

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

python Flask-在服务器上找不到请求的URL。(但为什么?),python,flask,Python,Flask,老实说,我不知道我应该问什么问题。也许有人可以编辑我的标题以便更具描述性 我有一个python flask应用程序,有时会返回: 在服务器上找不到请求的URL。如果您手动输入URL,请检查拼写并重试。 这种情况何时发生是有规律的,但我不知道为什么会发生 我有一个主页,用户单击谷歌地图上的一个标记,然后单击一个超链接“查看天气预报”按钮,这会将他们带到天气预报页面: 在我的主python文件(相当于index.py)中,我为下一页创建路由,如下所示: @app.route("/weat

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

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

在服务器上找不到请求的URL。如果您手动输入URL,请检查拼写并重试。

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

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

在我的主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))
此URL不起作用:

http://localhost:5000/weather_forecast/Mount%20Yonah/Tallulah%20Gorge/4188197/34.637/-83.72200000000001/Clarkesville
我不希望有一个神奇的代码修复,但有人能建议我还可以寻找什么?或者在我的问题中包含哪些其他信息会有帮助。

允许的URL是:
/weather\u forecast//

您可能正在尝试访问URL:

/weather\u forecast//

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


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

我想,您需要设置正确的精度来存储谷歌地图
lat
lon

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

..

    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\u forecast()
函数的代码,它返回的数据可以在
weather\u forecast.html
模板中使用与
image\u 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)
只需发送
wf
对象,然后在
weather\u forecast.html
模板中使用它,就像:

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

我找到了解决办法。原来我在不知不觉中传递了一个错误的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.722000000000001/Clarkesville

区别在于,在我的
weather\u forecast()
函数中,我创建了如下路线:
@app.route(“/weather\u forecast//”

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


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

返回语句的缩进不正确。返回语句末尾没有
/
url@ErichPurpur请提供您试图访问的URL。以下是一个示例:
http://localhost:5000/weather_forecast/Mount%20Yonah/Tallulah%20Gorge/4188197/34.637/-83.722000000000001/Clarkesville
@ErichPurpur你是点击错误的URL。您正在使用的URL发送了6个变量,而URL只允许5个。@ErichPurpur刚刚看到您找到了答案。很抱歉,我混淆了django和flask模型。DecimalField,我将更新我的答案,我在django项目中使用了该代码,所以我刚刚将flask的想法与正确的moeld字段相适应谢谢,为什么这样做更好:
而不是我之前所做的?记住少即是多,只需将基本参数传递到
渲染模板()
,您就可以实现更多,您有5个参数(
每日天气预报
扩展天气预报
历史天气预报
日期
图像url
)可以“减少”为1个参数
wf```除了它们需要(
城市id
lat
lon`和
天气站点
)之外,还可以传递给函数。
                       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)
<img src="{{ wf.image_choice(lat, lon, city_id) }}" atl="">
{{ config.GOOGLE_MAPS_JS_API_KEY }}