I am learning python and testing the new things, but I ran into a template error this morning, and so far I haven't found any answers to the problem.
contents of the python file:
<!doctype html>
<html>
<body>
<h1>Hello {{ name }}!</h1>
</body>
</html>
but when I put in http://127.0.0.1:5000/hello/user, the python shell gives this error:
contents of the python file:
from flask import Flask, render_template app = Flask(__name__, template_folder='template') @app.route('/hello/<user>') def hello_name(user): return render_template('hello.html', name=user) if __name__ == '__main__': app.run()contents of the HTML file that is used as template:
<!doctype html>
<html>
<body>
<h1>Hello {{ name }}!</h1>
</body>
</html>
but when I put in http://127.0.0.1:5000/hello/user, the python shell gives this error:
Error:[2020-08-26 18:05:51,076] ERROR in app: Exception on /hello/user [GET]
Traceback (most recent call last):
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\myname\Desktop\Python\module projects\flask\app\hello.py.py", line 6, in hello_name
return render_template('hello.html', name=user)
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\flask\templating.py", line 138, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\jinja2\environment.py", line 930, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\jinja2\environment.py", line 883, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\jinja2\environment.py", line 857, in _load_template
template = self.loader.load(self, name, globals)
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\jinja2\loaders.py", line 115, in load
source, filename, uptodate = self.get_source(environment, name)
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\flask\templating.py", line 60, in get_source
return self._get_source_fast(environment, template)
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\flask\templating.py", line 89, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: hello.html
127.0.0.1 - - [26/Aug/2020 18:05:51] "[35m[1mGET /hello/user HTTP/1.1[0m" 500
I have tried putting in the [template_folder] option, putting the whole path in there, and have my file structure as it should be, with the HTML file in a subdirectory called templates. First I did it without any virtual environment but put it all in one, but even now it does not work. Are there people who experienced the same?