samar musthafa
samar musthafa

Reputation: 95

Flask throws TemplateNotFound even when required template is in /templates

I'm working on a Flask based Webapp. When I ran my app.py on localhost:5000/ it works for the first three pages. I can navigate home>users>viewuser. But when i launch the hub.html file from viewuser, jinja threw a TemplateNotFound error. I do have a templates folder. The other webpages are rendered from the same folder. Why is it throwing an exception at just this page?

called function in app.py:

@app.route("/hub")
def hub():
    users = service.pullusers()
    return render_template('hub', ulist=users)

caller function in viewuser.html

<div>
   <br><br><br><a class="btn btn-primary btn-xl" href={{ url_for('hub')}}>Transfer Credits</a>
</div>

file tree:

C:.
│   a.txt
│   app.py
│   main.css
│   models.py
│   service.py
│
├───static
│   ├───css
│   │       custom-responsive-styles.css
│   │       main.css
│   │       styles.css
│   │
│   └───js
│           all-plugins.js
│           jquery-3.2.1.min.js
│           plugins-activate.js
│
├───templates
│       home.html
│       hub.html
│       sendcreds.html
│       users.html
│       viewuser.html
│
└───__pycache__
        models.cpython-37.pyc
        service.cpython-37.pyc

Error:


jinja2.exceptions.TemplateNotFound

jinja2.exceptions.TemplateNotFound: hub
Traceback (most recent call last)

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2464, in __call__

    return self.wsgi_app(environ, start_response)

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2450, in wsgi_app

    response = self.handle_exception(e)

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1867, in handle_exception

    reraise(exc_type, exc_value, tb)

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\_compat.py", line 39, in reraise

    raise value

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2447, in wsgi_app

    response = self.full_dispatch_request()

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request

    rv = self.handle_user_exception(e)

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1821, in handle_user_exception

    reraise(exc_type, exc_value, tb)

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\_compat.py", line 39, in reraise

    raise value

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request

    rv = self.dispatch_request()

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1936, in dispatch_request

    return self.view_functions[rule.endpoint](**req.view_args)

    File "C:\Users\samar\Documents\TSF\app.py", line 26, in hub

    return render_template('hub', ulist=users)

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\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\samar\AppData\Local\Programs\Python\Python37\lib\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\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\jinja2\environment.py", line 883, in get_template

    return self._load_template(name, self.make_globals(globals))

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\jinja2\environment.py", line 857, in _load_template

    template = self.loader.load(self, name, globals)

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\jinja2\loaders.py", line 115, in load

    source, filename, uptodate = self.get_source(environment, name)

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\templating.py", line 60, in get_source

    return self._get_source_fast(environment, template)

    File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\templating.py", line 89, in _get_source_fast

    raise TemplateNotFound(template)

    jinja2.exceptions.TemplateNotFound: hub

The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.

To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

    dump() shows all variables in the frame
    dump(obj) dumps all that's known about the object

Brought to you by DON'T PANIC, your friendly Werkzeug powered traceback interpreter.

What am i doing wrong? Please help me fast. I'm on a roll.

Upvotes: 1

Views: 383

Answers (1)

Mr. lindroid
Mr. lindroid

Reputation: 181

You need to write the template name with extension. Or you can say as it is. write 'hub.html' instead of 'hub' in render template. And you also need to return a ok status code (200).

@app.route("/hub")
def hub():
    users = service.pullusers()
    return render_template('hub.html', ulist=users), 200

Upvotes: 1

Related Questions