Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TemplateNotFound error with flask
#1
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?
Reply
#2
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>
Reply
#3
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
Reply
#4
(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.
Reply
#5
Thank you, at least the debugger works and gave me the url
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Server Error with parse_args() using Flask NoNameoN 0 1,063 Jul-30-2022, 09:42 AM
Last Post: NoNameoN
  [split] flask 500 internal server error DarthTensor 3 3,949 Nov-11-2021, 06:10 AM
Last Post: DarthTensor
  Error updating one to many relationship in Flask/ SQLAlchemy atindra 0 3,300 Apr-15-2021, 10:29 PM
Last Post: atindra
  Running flask run produces error. Charles1 1 4,028 Oct-04-2019, 10:38 PM
Last Post: snippsat
  [Flask] html error 405 SheeppOSU 0 2,319 Jun-08-2019, 04:42 PM
Last Post: SheeppOSU
  [flask] mail.send giving error SheeppOSU 2 2,829 May-31-2019, 09:31 PM
Last Post: SheeppOSU
  About Error (Flask) bescf 1 2,624 Mar-20-2019, 08:18 PM
Last Post: nilamo
  Flask Error-Could not build url for endpoint 'index'. Did you forget to specify value Prince_Bhatia 0 10,273 Feb-25-2019, 05:52 AM
Last Post: Prince_Bhatia
  Flask assertion error mebaysan 1 4,485 Feb-07-2019, 08:34 PM
Last Post: pascale
  Flask: Error output to the browser instead of error_log nikos 1 2,735 Sep-28-2018, 12:49 PM
Last Post: thomasp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020