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

如何在PythonAnywhere新项目中解决Flask render_template文件找不到问题?

解决PythonAnywhere中render_template找不到模板文件的方案
  • 先确认项目目录结构是否符合Flask规范,正确结构应为:

    mysite/
        app.py  # Flask主运行文件
        templates/
            testFile.html
    
  • 不要使用绝对路径或多级相对路径调用render_template,Flask默认会自动从当前运行脚本所在目录的templates子目录查找文件,直接传入文件名即可:

    render_template("testFile.html")
    
  • 如果Flask应用初始化时未自动识别到模板目录,可手动指定template_folder参数:

    from flask import Flask
    app = Flask(__name__, template_folder="/home/myusername/mysite/templates")
    
  • 检查文件及目录权限:确保PythonAnywhere的运行用户拥有templates目录和testFile.html文件的读取权限,可通过控制台执行以下命令设置:

    chmod 755 /home/myusername/mysite/templates
    chmod 644 /home/myusername/mysite/templates/testFile.html
    
  • 验证当前工作目录:在Flask脚本中加入代码打印当前工作目录,确认路径是否与预期一致:

    import os
    print(os.getcwd())  # 查看控制台输出的当前目录
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 19:19:54