【问题标题】:Internal server error while using flask in cs50 ide在 cs50 ide 中使用烧瓶时出现内部服务器错误
【发布时间】:2020-12-31 05:35:52
【问题描述】:

代码全部正确,但无法在浏览器上显示所需的输出 - 浏览器警告内部服务器消息(500)并且终端显示以下错误:

~/tracks/ $ flask run * Serving Flask app "application.py" (lazy 加载)*环境:开发*调试模式:关闭*运行 https://3405db29-f322-4a37-845f-11d0e562946b-ide.cs50.xyz:8080/(按 CTRL+C 退出)* 使用 stat 重新启动 [2020-09-13 07:29:50,889] 应用程序中的错误:/ [GET] Traceback 上的异常(最后一次调用): 文件“/usr/local/lib/python3.7/site-packages/flask/app.py”,第 2446 行, 在 wsgi_app response = self.full_dispatch_request() 文件“/usr/local/lib/python3.7/site-packages/flask/app.py”,第 1951 行,在 full_dispatch_request rv = self.handle_user_exception(e) 文件“/usr/local/lib/python3.7/site-packages/flask/app.py”,第 1820 行,在 处理用户异常 reraise(exc_type, exc_value, tb) 文件“/usr/local/lib/python3.7/site-packages/flask/_compat.py”,第 39 行,在 再加注 提高价值文件“/usr/local/lib/python3.7/site-packages/flask/app.py”,第 1949 行,在 full_dispatch_request rv = self.dispatch_request() 文件“/usr/local/lib/python3.7/site-packages/flask/app.py”,第 1935 行,在 dispatch_request 返回 self.view_functionsrule.endpoint 文件“/home/ubuntu/tracks/application.py”,第 10 行,在索引中 return render_template("index.html", number=number) 文件 "/usr/local/lib/python3.7/site-packages/flask/templating.py", 行 138,在渲染模板中 ctx.app.jinja_env.get_or_select_template(template_name_or_list),文件“/usr/local/lib/python3.7/site-packages/jinja2/environment.py”, 第 930 行,在 get_or_select_template 中 返回 self.get_template(template_name_or_list, parent, globals) 文件“/usr/local/lib/python3.7/site-packages/jinja2/environment.py”, 第 883 行,在 get_template 中 返回 self._load_template(name, self.make_globals(globals)) 文件 "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", 第 857 行,在 _load_template 模板 = self.loader.load(self, name, globals) 文件“/usr/local/lib/python3.7/site-packages/jinja2/loaders.py”,第 117 行, 在负载 源,文件名,最新 = self.get_source(环境,名称)文件“/usr/local/lib/python3.7/site-packages/flask/templating.py”, 第 60 行,在 get_source
返回self._get_source_fast(环境,模板)文件“/usr/local/lib/python3.7/site-packages/flask/templating.py”,第89行, 在 _get_source_fast
raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: index.html
192.168.74.236 - - [13/Sep/2020 07:29:50] “GET / HTTP/1.0”500 -

【问题讨论】:

    标签: python-3.x flask web-applications cs50 internal-server-error


    【解决方案1】:

    除了@Grey Li 说的,你的index.html模板也找不到,因为:

    • 您的模板文件夹中没有index.html 文件或
    • index.html 文件的路径不正确。

    模板不存在

    如果文件不存在,则需要创建它:

    $ touch index.html 
    

    考虑您当前的工作目录,以确保您在正确的位置创建它。上面的示例假设您位于终端的 templates 子文件夹中。否则,您可能需要将 templates 子文件夹的路径添加为:

    $ touch app/templates/index.html
    
    # wherever you might be
    

    路径不正确

    只需修改render_template 中的路径。假设您正在使用一个应用程序工厂,其中每个模块都有自己的模板子文件夹。你会这样做:

    @bp.route('/login', methods=['GET', 'POST'])
    def login():
       # ...
       return render_template('auth/index.html')
    

    【讨论】:

      【解决方案2】:

      程序正在~/tracks目录下名为templates的目录中查找名为index.html的文件,但未找到。

      【讨论】:

        【解决方案3】:

        回溯通常提供有关问题的有用信息。在您的终端输出中,您可以看到“raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: index.html”,然后您需要检查您的视图函数是否返回一个名为index.html 的模板。您要么需要在templates 文件夹下创建模板index.html,要么更改return 语句。

        此外,您可以使用FLASK_ENV 启用调试模式:

        $ export FLASK_ENV=development
        $ flask run
        

        然后漂亮的 trackback 将显示在浏览器上,而不是 500 错误页面。

        【讨论】:

          猜你喜欢
          • 2014-08-06
          • 1970-01-01
          • 1970-01-01
          • 2019-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-24
          • 2019-11-09
          相关资源
          最近更新 更多