You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决基于Flask的房间数预测器点击预测时的404错误

解决Flask房屋房间数预测器的404错误问题

问题描述

开发了基于机器学习的房屋房间数预测器,通过价格和面积预测房间数量,但点击预测按钮时出现404错误:

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

错误原因及修复方案

1. Flask路由路径语法错误

预测路由的路径末尾多了一个不必要的逗号,导致实际路由为/predict,,但HTML表单提交的路径是/predict,两者不匹配触发404。

错误代码:

@app.route('/predict,', methods=['POST'])

修复后代码:

@app.route('/predict', methods=['POST'])

2. HTML表单输入标签语法错误

input标签的name属性被错误拆分到单独行,导致表单无法正确传递参数,即使路由修复,后端也无法获取price和area的值。

错误代码片段:

<input type="text" id="price">
name="price"><br>
<input type="text" id="area">
name="area"><br>

修复后代码片段:

<input type="text" id="price" name="price"><br>
<input type="text" id="area" name="area"><br>

3. 模型预测参数类型与顺序错误

  • 表单获取的price和area是字符串类型,需转换为数值类型才能传入模型;
  • 训练时特征顺序为['area','price'],预测时需保持一致,否则结果会偏差。

修复后的predict函数代码:

@app.route('/predict', methods=['POST'])
def predict():
    price = float(request.form['price'])
    area = float(request.form['area'])
    
    # 保持与训练时的特征顺序一致:area在前,price在后
    rooms = model.predict([[area, price]])[0]
    
    # 房间数取整更符合实际场景
    return render_template('predict.html', rooms=round(rooms))

4. 额外注意事项

  • 确保predict.html模板文件存在于项目的templates目录下,模板中需正确渲染结果,示例:
    <h2>预测结果:{{ rooms }} 个房间</h2>
    
  • 确认filtered.xlsx文件放置在Flask项目根目录下,路径无误。

内容的提问来源于stack exchange,提问作者Rasim Dilbani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 20:45:23