Python Forum

Full Version: TemplateNotFound error with flask
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
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] "GET /hello/user HTTP/1.1" 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?
You don't need to give path to templates,just make a folder.
my_site\
  |-- app.py
  templates\
    |-- hello.html
  static\
    # CSS,js,images...ect
app.py
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/hello/<user>')
def hello_name(user):
   return render_template('hello.html', name=user)

if __name__ == '__main__':
   app.run(debug=True) # Have always on debug when you develop,before finish version
hello.py
<!doctype html>
<html>
  <body>
    <h1>Hello {{ name }}!</h1>
  </body>
</html>
First, thank you very much for your reply,

I have already set up the folders, even the static folder although I did not include any CSS code yet. Also, when I set debug=True, I get this:

* Serving Flask app "app" (lazy loading)
* Environment: production
 WARNING: This is a development server. Do not use it in a production deployment.
 Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat

without any local host url (but to be honest, that is a lesser problem for me right now) but I am getting a bit hopeless because of the uncommon template error
(Aug-27-2020, 06:17 AM)Veztar Wrote: [ -> ]without any local host url (but to be honest, that is a lesser problem for me right now) but I am getting a bit hopeless because of the uncommon template error
How are running this?
Here a run from my_site folder,that can placed anywhere you want.
G:\all_flask\2020\div\my_site
λ ls
app.py  static/  templates/

G:\all_flask\2020\div\my_site
λ python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 113-254-199
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
I use cmder here,but just same in cmd can show it there to.
C:\Users\Tom>cd G:\all_flask\2020\div\my_site

C:\Users\Tom>g:

G:\all_flask\2020\div\my_site>dir
 Volume in drive G is Seagate Backup Plus Drive
 Volume Serial Number is E687-60FA

 Directory of G:\all_flask\2020\div\my_site

26.08.2020  20:01    <DIR>          .
26.08.2020  20:01    <DIR>          ..
26.08.2020  20:08               225 app.py
26.08.2020  20:01    <DIR>          static
26.08.2020  20:07    <DIR>          templates
               1 File(s)            225 bytes
               4 Dir(s)  1 953 557 790 720 bytes free

G:\all_flask\2020\div\my_site>python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 113-254-199
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

G:\all_flask\2020\div\my_site>
As you see it now running localhost on port 5000,that can accessed in browser.
Thank you, at least the debugger works and gave me the url