Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a html template from a different directory in python flask

@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's this code. How would I reference the template that's in project/frontend/src/view_notifications.html I've tried using .. but it keeps saying the path isn't found. Is there another way I should be doing 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 
like image 767
BigBoy Avatar asked Jun 23 '15 12:06

BigBoy


People also ask

How do I connect templates in Flask?

html template file in a directory called templates inside your flask_app directory. Flask looks for templates in the templates directory, which is called templates , so the name is important. Make sure you're inside the flask_app directory and run the following command to create the templates directory: mkdir templates.

Where is the template folder in Flask?

Folder structure for a Flask app Everything the app needs is in one folder, here named my-flask-app. That folder contains two folders, specifically named static and templates. The static folder contains assets used by the templates, including CSS files, JavaScript files, and images.

What is static folder in Flask?

Flask – Static Files Usually, the web server is configured to serve them for you, but during the development, these files are served from static folder in your package or next to your module and it will be available at /static on the application. A special endpoint 'static' is used to generate URL for static files.


2 Answers

Flask is looking in templates/frontend/src/view_notifications.html for your template file. You either need to move your templates file to that location or change the default template folder.

According to the Flask docs you can specify a different folder for your templates. It defaults to templates/ in the root of your app:

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

UPDATE:

After testing it myself on a Windows machine the templates folder does need to be named 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 of 'templates' to 'src' and renaming the templates folder to 'src' resulted in the same error OP received.

like image 83
kylieCatt Avatar answered Sep 20 '22 16:09

kylieCatt


a batter solution is to just go directly without os.path.abspath like this:

from flask import Flask  app = Flask(__name__, template_folder='../../frontend/src') 
like image 36
Macilias Avatar answered Sep 24 '22 16:09

Macilias