2

I have this structure,

index.py
run.py
app/
  __init__.py
  routes.py
  templates/
    ...

index.py,

import os
import sys

activate_this = os.path.dirname(__file__) + '/venv/Scripts/activate_this.py'
exec(open(activate_this).read(), dict(__file__ = activate_this))

# Expand Python classes path with your app's path.
sys.path.insert(0, os.path.dirname(__file__))

from run import app

#Initialize WSGI app object.
application = app

run.py,

from flask import Flask

app = Flask(__name__)

from app import routes

if __name__ == '__main__':
    app.run()

app/routes.py,

from run import app
from flask import Flask, render_template

@app.route('/')
def hello_world():
    return 'Hello World'

@app.route('/dave')
def myDave():
    return 'Hello World - From Dave'

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

@app.route('/about')
def about():
  return render_template('about.html')

app/__init__.py,

(blank)

So when I access the app with / I get Hello World which is correct, and /dave I get Hello World - From Dave

But with /home and /about, I get 500 Internal Server Error

The log file does not give much info about the error at all,

[Fri Aug 21 19:47:06.992431 2015] [mpm_winnt:notice] [pid 7036:tid 244] AH00418: Parent: Created child process 5872 [Fri Aug 21 19:47:07.257631 2015] [wsgi:warn] [pid 5872:tid 244] mod_wsgi: Compiled for Python/2.7.9+. [Fri Aug 21 19:47:07.257631 2015] [wsgi:warn] [pid 5872:tid 244] mod_wsgi: Runtime using Python/2.7.10. [Fri Aug 21 19:47:07.273231 2015] [mpm_winnt:notice] [pid 5872:tid 244] AH00354: Child: Starting 64 worker threads.

But it seems that the module render_template from Flask is not loaded or not working.

Any ideas what have I done wrong?

Run
  • 54,938
  • 169
  • 450
  • 748
  • I have set that on `app.run(debug = True)` but it is not doing anything. I still get the same 500 error as before. – Run Aug 21 '15 at 13:12

1 Answers1

4

Flask defaults to a 'templates' folder at the root path of the application.

Given your existing setup, you can instantiate your Flask app like this in run.py:

project_root = os.path.dirname(__file__)
template_path = os.path.join(project_root, 'app/templates')
app = Flask(__name__, template_folder=template_path)
chishaku
  • 4,577
  • 3
  • 25
  • 33
  • 1
    Except that you cannot give it a relative path. You would need to calculate an absolute path off an anchor of ``os.path.dirname(__file__)``. This is necessary as the current working directory will not be where your code is. – Graham Dumpleton Aug 21 '15 at 21:01