I'm having same issue as described in these two questions/answers:
Python WSGI + Flask render_template - 500 Internal Server Error?
Getting a 500 Internal Server Error using render_template and Flask
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
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.
There are different ways in Flask to render 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.
What you have written in the about
route is the render_template
function. The way render_template
works is by reading an HTML file with the name you pass as a parameter and renders that template. 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.
Create an about.html
file at the same level as your app.py file and type in something in it and try to render. It should work.
To read more on render_template
check out the documentation here or read the getting started tutorial for Flask here