简体   繁体   中英

Python WSGI + Flask render_template - 500 Internal Server Error?

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM