简体   繁体   English

即使按照几个类似问题中的建议进行修复,也会出现 500 内部服务器错误

[英]Getting a 500 Internal Server Error even with fixes as recommended in a couple of similar questions

I'm having same issue as described in these two questions/answers:我遇到了与这两个问题/答案中描述的相同的问题:

Python WSGI + Flask render_template - 500 Internal Server Error? Python WSGI + Flask render_template - 500 内部服务器错误?

Getting a 500 Internal Server Error using render_template and Flask 使用 render_template 和 Flask 获取 500 内部服务器错误

and I've narrowed it to the following:我把它缩小到以下几点:

Here's the file structure:这是文件结构:

../project/
    project.py
    templates/
        about.html

I'm still getting this error (shorten):我仍然收到此错误(缩短):


● project.service - Gunicorn instance to serve project
   Loaded: loaded (/etc/systemd/system/project.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2020-02-15 06:28:46 UTC; 3min 28s ago
 Main PID: 3942 (gunicorn)
    Tasks: 4 (limit: 1151)
   CGroup: /system.slice/project.service
           ├─3942 /home/dev/project/project_env/bin/python3.6 /home/dev/project/project_env/bin/gunicorn --workers 3 --bind unix:project.sock -m 007 wsgi
           ├─3967 /home/dev/project/project_env/bin/python3.6 /home/dev/project/project_env/bin/gunicorn --workers 3 --bind unix:project.sock -m 007 wsgi
           ├─3968 /home/dev/project/project_env/bin/python3.6 /home/dev/project/project_env/bin/gunicorn --workers 3 --bind unix:project.sock -m 007 wsgi
           └─3969 /home/dev/project/project_env/bin/python3.6 /home/dev/project/project_env/bin/gunicorn --workers 3 --bind unix:project.sock -m 007 wsgi

Feb 15 06:29:17 project gunicorn[3942]:     return self._load_template(name, self.make_globals(globals))
Feb 15 06:29:17 project gunicorn[3942]:   File "/home/dev/project/project_env/lib/python3.6/site-packages/jinja2/environment.py", line 857, in _load_template
Feb 15 06:29:17 project gunicorn[3942]:     template = self.loader.load(self, name, globals)
Feb 15 06:29:17 project gunicorn[3942]:   File "/home/dev/project/project_env/lib/python3.6/site-packages/jinja2/loaders.py", line 117, in load
Feb 15 06:29:17 project gunicorn[3942]:     source, filename, uptodate = self.get_source(environment, name)
Feb 15 06:29:17 project gunicorn[3942]:   File "/home/dev/project/project_env/lib/python3.6/site-packages/flask/templating.py", line 60, in get_source
Feb 15 06:29:17 project gunicorn[3942]:     return self._get_source_fast(environment, template)
Feb 15 06:29:17 project gunicorn[3942]:   File "/home/dev/project/project_env/lib/python3.6/site-packages/flask/templating.py", line 89, in _get_source_fast
Feb 15 06:29:17 project gunicorn[3942]:     raise TemplateNotFound(template)
Feb 15 06:29:17 project gunicorn[3942]: jinja2.exceptions.TemplateNotFound: about.html

So the issue as far as I can tell, is that last line所以据我所知,问题是最后一行

Feb 15 06:29:17 project gunicorn[3942]: jinja2.exceptions.TemplateNotFound: about.html

I don't understand why, even though the files/folders are there.我不明白为什么,即使文件/文件夹在那里。 The closest I could find is this comment:我能找到的最接近的是这个评论:

https://stackoverflow.com/a/56589998/12070612 https://stackoverflow.com/a/56589998/12070612

ENV = jinja2.Environment(loader=jinja2.FileSystemLoader(str(root_path / 'templates')))
template = ENV.get_template(your_template_name)

But if I add the "ENV=..." lines to my project.py file, then Gunicorn complaints and fails to start process.但是,如果我将“ENV=...”行添加到我的 project.py 文件中,那么 Gunicorn 会抱怨并且无法启动进程。

There are different ways in Flask to render HTML.在 Flask 中有不同的方式来呈现 HTML。 You could simply generate that HTML directly from the Python code similar to what you have done for the home route or you could use a templating language like Jinja.您可以简单地直接从 Python 代码生成该 HTML,类似于您为home路线所做的工作,或者您可以使用像 Jinja 这样的模板语言。

What you have written in the about route is the render_template function.你在about路由中写的是render_template函数。 The way render_template works is by reading an HTML file with the name you pass as a parameter and renders that template. render_template工作方式是读取带有您作为参数传递的名称的 HTML 文件并呈现该模板。 In you case, you have called the render_template method and asked it to render a template called about.html but since the template does not exist, it fails to load and throws an exception.在您的情况下,您调用了render_template方法并要求它呈现一个名为about.html的模板,但由于该模板不存在,它无法加载并引发异常。

Create an about.html file at the same level as your app.py file and type in something in it and try to render.创建一个与 app.py 文件相同级别的about.html文件,然后在其中输入一些内容并尝试渲染。 It should work.它应该工作。

To read more on render_template check out the documentation here or read the getting started tutorial for Flask here要阅读有关render_template更多信息,请查看此处的文档或阅读此处的 Flask 入门教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM