64
@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
vvvvv
  • 25,404
  • 19
  • 49
  • 81
BigBoy
  • 803
  • 1
  • 10
  • 15

4 Answers4

73

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.

kylieCatt
  • 10,672
  • 5
  • 43
  • 51
34

A better solution is to just go directly without os.path.abspath like this:

from flask import Flask

app = Flask(__name__, template_folder='../../frontend/src')
Oli
  • 9,766
  • 5
  • 25
  • 46
Macilias
  • 3,279
  • 2
  • 31
  • 43
  • 2
    In what way is using relative paths better? I'm not disagreeing but you should explain why in your answer. – kylieCatt Feb 11 '20 at 18:32
  • 1
    because you can skip the os.path.abspath usage, within your presented solution you use relative path too and creates a absolute one based on that, but it works also without the conversion. – Macilias Feb 12 '20 at 19:25
  • 1
    Right, it works but now if the file structure changes it requires a code change. You're trading maintainability/reliability for readability/ease. There is nothing wrong with that but it's not necessarily "better"; it's just different. – kylieCatt Feb 12 '20 at 21:11
  • 2
    If the file structure changes, you need to adjust the code in your Solution as well. You are also using the same relativ path so my solution only adds readability but does not introduce additional maintenance overhead. – Macilias Feb 14 '20 at 07:20
1

It seems that Flask follows symlinks when searching for templates.

I put a symlink in the templates directory that points to my other template directory.

aronbo
  • 303
  • 4
  • 9
-1

Flask will automatically detects folder name "templates". so you should create a folder called templates in project directory where your .py application file is present. Then place folder which contain html file inside templates folder. Hence your code will be

return render_template("frontend/src/view_notifications.html", posts=posts)

Ensure the path "frontend/src/view_notifications.html" is inside templates folder

  • 1
    That's not a valid answer. That flask uses a template folder is the problem and wants to be avoided. – france1 Aug 06 '22 at 05:39
  • Not a valid answer. The question is about looking for templates other than the default "templates" folder – Kedar Joshi Nov 01 '22 at 20:12