我有这个结构,
index.py
run.py
app/
__init__.py
routes.py
templates/
...
索引.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
运行.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')
应用程序/__init__.py
,
(blank)
因此,当我使用 /
访问应用程序时,我得到 Hello World
这是正确的,而 /dave
我得到 Hello World - 来自戴夫
但是使用 /home
和 /about
时,我收到 500 内部服务器错误
日志文件根本没有提供有关错误的太多信息,
[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
似乎未加载或无法工作。
有什么想法我做错了什么吗?
最佳答案
Flask 默认为应用程序根路径下的"template"文件夹。
根据您现有的设置,您可以在 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)
关于Python WSGI + Flask render_template - 500 内部服务器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32140116/