Python WSGI + Flask render_template - 500 Internal Server Error?

喜欢而已 提交于 2019-12-11 03:49:16

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/32140116/python-wsgi-flask-render-template-500-internal-server-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!