Flask: render_template with path

Be sure the Python file and Template folder are all under the same working directory folder.


The template folder can be specified when creating the Flask app (or Blueprint):

from flask import Flask
app = Flask(__name__, template_folder='folder1')

Source: http://flask.pocoo.org/docs/0.12/api/#application-object

from flask import Blueprint
auth_blueprint = Blueprint('auth', __name__, template_folder='folder1')

Source: http://flask.pocoo.org/docs/0.12/blueprints/#templates

The template_folder is relative to where the app/blueprint is located. Use the os library to create paths to template folders outside of the app/blueprint directory.

eg.

import os
APP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(APP_PATH, 'templates/')
  • run from subdirectory of app root
  • APP_PATH retrieves parent directory path (app root)

Tags:

Flask