Python WSGI + Flask render_template-500内部服务器错误? [英] Python WSGI + Flask render_template - 500 Internal Server Error?

查看:186
本文介绍了Python WSGI + Flask render_template-500内部服务器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个结构,

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,

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)

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

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

但是使用/home/about时,出现 500 Internal Server Error

日志文件根本没有提供有关该错误的太多信息,

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

[2015年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 +编译. [2015年8月21日星期五19:47:07.257631] [wsgi:warn] [pid 5872:tid 244] mod_wsgi:使用Python/2.7.10的运行时. [2015年8月21日星期五19:47:07.273231] [mpm_winnt:notice] [pid 5872:tid 244] AH00354:孩子:正在启动64个工作线程.

[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.

但是,似乎Flask中的模块render_template未加载或无法正常工作.

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

有什么主意我做错了吗?

Any ideas what have I done wrong?

推荐答案

Flask默认位于应用程序根目录下的"templates"文件夹中.

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

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

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)

这篇关于Python WSGI + Flask render_template-500内部服务器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆