-1

Flask is giving template not found exception on render_template() method

enter image description here

.

routes.py

from flask import render_template
from app import app

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html', title='Home', user='user')
    # return render_template('index.html')

index.html

<html>
    <head>
        <title>{{ title }} </title>
    </head>
    <body>
        <h1>Hello, {{ user }}!</h1>
    </body>
</html>

init.py

from flask import Flask
app=Flask(__name__)
from app import routes

bot.py

from app import app

error log I am getting

[2018-10-11 15:00:25,349] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "D:\Bot\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "D:\Bot\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "D:\Bot\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "D:\Bot\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "D:\Bot\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "D:\Bot\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\Bot\app\routes.py", line 8, in index
    return render_template('index.html')
  File "D:\Bot\venv\lib\site-packages\flask\templating.py", line 134, in render_template
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "D:\Bot\venv\lib\site-packages\jinja2\environment.py", line 869, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "D:\Bot\venv\lib\site-packages\jinja2\environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "D:\Bot\venv\lib\site-packages\jinja2\environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "D:\Bot\venv\lib\site-packages\jinja2\loaders.py", line 113, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "D:\Bot\venv\lib\site-packages\flask\templating.py", line 58, in get_source
    return self._get_source_fast(environment, template)
  File "D:\Bot\venv\lib\site-packages\flask\templating.py", line 86, in _get_source_fast
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: index.html
127.0.0.1 - - [11/Oct/2018 15:00:25] "GET / HTTP/1.1" 500 -

I have already tried answer of this question, but still not working.

Somebody help me with this, Thank you...

muhammed aslam
  • 44
  • 1
  • 11

1 Answers1

0

you need the templates directory move to the app directory.

why?

because templates directory and the file where the app(the instance of Flask) is located in the same directory.

app = Flask(__name__)

inside the Flask, the jinja2 environment rely on the __name__ parameter to load your templates.

ltoddy
  • 121
  • 1
  • 3
  • 6