Flask app using app factory can't find template

SaiBorg

I am using an app factory and blueprints to modularize my application. The templates folder is located under the app package and contains the index.html template. However, I get a TemplateNotFoundError when viewing the page. Why isn't my template found?

project/
├── app
│   ├── __init__.py
│   └── templates
│       └── index.html
└── run.py

app/__init__.py

from flask import Flask, Blueprint, render_template

def create_app():
    app = Flask('__name__')
    app.register_blueprint(home)
    return app

home = Blueprint('home', __name__)

@home.route('/')
def homepage():
    return render_template('home/index.html')

run.py

from app import create_app
app = create_app()
app.run()
File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/werkzeug/serving.py", line 267, in run_wsgi
    execute(self.server.app)
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/werkzeug/serving.py", line 255, in execute
    application_iter = app(environ, start_response)
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/saikrishnamohan/Projects/FlaskVueApp/dream-team/app/home/views.py", line 12, in homepage
    return render_template('home/index.html', title="Welcome")
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/templating.py", line 133, in render_template
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/jinja2/environment.py", line 869, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/jinja2/loaders.py", line 113, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/templating.py", line 57, in get_source
    return self._get_source_fast(environment, template)
  File "/Users/saikrishnamohan/Projects/VirtualEnvs/flaskVue/lib/python2.7/site-packages/flask/templating.py", line 85, in _get_source_fast
    raise TemplateNotFound(template)
TemplateNotFound: home/index.html
davidism

You have a typo: Flask('__name__') should not have quotes around __name__. Fix the typo so that it's app = Flask(__name__).

The argument, import_name, passed to Flask lets it figure out where directories like the templates folder is. Since the string '__name__' doesn't actually describe where your app is, Flask assumes the current working directory. But you're running from the directory above your package, so the templates folder can't be found.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can't FLASK_APP find my factory function?

Flask App Running In Docker Can't Find Template

test flask app using pytest can't find fixture client

Flask can't find app file

Django App template Loader, can't find app templates

Flask config not being loaded using FLASK_APP and App Factory

Access @App decorator in Flask using Factory Pattern

PyCharm can't find Flask template that exists

How to put decorators on `app` when using Application Factory with Flask?

Flask `app.before_request` when using a factory function?

Define models in seperate file when using app factory flask

How to access Flask app's context from within embedded Dash app when using app factory pattern?

using angularjs and json wordpress api can't access currentPage value in app.factory $resource

Failed to find Flask application or factory in module "config" Use "FLASK_APP=config:name to specify one

Why is my flask web app not working properly? I can't find any errors myself

Flask can't find asset(css, js...) files of Vue-built app

Intent chooser can't find app to handle

Django can't find static files for an app

Can't find react Native app domain

Can't find folder after publish app

App crashing due to NSInvalidArgumentException but can't find it

Angular app can't find controller on transclude

Dockerfile can't find app.py

Django Can't Find App Templates

Can't find bootstrap file in Angular app

Flask - avoid circular imports with app factory

Can't find my app on App Store anymore

Flask: Template in Blueprint Inherit from Template in App?

flask server won't run if I named my application factory function other than "create_app"