首页 文章

Python WSGI Flask render_template - 500内部服务器错误?

提问于
浏览
2

我有这个结构,

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()

应用程序/ 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)

所以,当我使用 / 访问应用程序时,我得到 Hello World 这是正确的,而 /dave 我得到 Hello World - From Dave

但是 /home/about ,我得到 500 Internal Server Error

日志文件根本没有提供有关错误的详细信息,

[[8月21日星期五19:47:06.992431] [mpm_winnt:notice] [pid 7036:tid 244] AH00418:父母:创建子进程5872 [8月21日星期五19:47:07.257631 2015] [wsgi:warn] [pid 5872:tid 244] mod_wsgi:编译为Python / 2.7.9 . [星期五8月21日19:47:07.257631 2015] [wsgi:warn] [pid 5872:tid 244] mod_wsgi:使用Python / 2.7.10运行时 . [[星期五8月21日19:47:07.273231 2015] [mpm_winnt:notice] [pid 5872:tid 244] AH00354:Child:启动64个工作线程 .

但似乎Flask的模块 render_template 没有加载或无法正常工作 .

有什么想法我做错了什么?

1 回答

  • 4

    Flask默认为应用程序根路径上的“templates”文件夹 .

    鉴于您现有的设置,您可以在 run.py 中实例化您的Flask应用程序:

    project_root = os.path.dirname(__file__)
    template_path = os.path.join(project_root, 'app/templates')
    app = Flask(__name__, template_folder=template_path)
    

相关问题