How to link to html template from another directory in python bulb

@app.route('/view', methods=['GET', 'POST']) def view_notifications(): posts = get_notifications() return render_template("frontend/src/view_notifications.html", posts=posts) 

So in my project/backend/src/app.py there is this code. How can I refer to a template that I tried to use in project/frontend/src/view_notifications.html .. but it continues to say that the path was not found. Is there any other way I have to do this?

 [Tue Jun 23 12:56:02.597207 2015] [wsgi:error] [pid 2736:tid 140166294406912] [remote 10.0.2.2:248] TemplateNotFound: frontend/src/view_notifications.html [Tue Jun 23 12:56:05.508462 2015] [mpm_event:notice] [pid 2734:tid 140166614526016] AH00491: caught SIGTERM, shutting down 
+8
python directory flask path
source share
1 answer

The checkbox looks for templates/frontend/src/view_notifications.html for your template file. You need to either move the template file to this location or change the default templates folder.

According to the Flask docs, you can specify a different folder for your templates. By default, it is equal to templates/ in the root of your application:

 import os from flask import Flask template_dir = os.path.abspath('../../frontend/src') app = Flask(__name__, template_folder=template_dir) 

UPDATE:

After testing on a Windows computer, the templates folder should be called templates . This is the code I used:

 import os from flask import Flask, render_template template_dir = os.path.dirname(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))) template_dir = os.path.join(template_dir, 'frontend') template_dir = os.path.join(template_dir, 'templates') # hard coded absolute path for testing purposes working = 'C:\Python34\pro\\frontend\\templates' print(working == template_dir) app = Flask(__name__, template_folder=template_dir) @app.route("/") def hello(): return render_template('index.html') if __name__ == "__main__": app.run(debug=True) 

With this structure:

 |-pro |- backend |- app.py |- frontend |- templates |- index.html 

Changing any instance from 'templates' to 'src' and renaming the templates folder to 'src' led to the same error that the OP got.

+15
source share

All Articles