【发布时间】:2021-08-14 13:45:02
【问题描述】:
我是 Flask 初学者,如果我的问题的答案很明显,请见谅。
我一直在使用 OVH 服务器处理一个项目。我一直在使用本教程设置第一个“Hello world”:https://docs.ovh.com/gb/en/web-power/python-install-flask/
这是我的项目的结构:
www/
venv/
myapp.py
此时,一切正常。当我连接到我的网站时,它显示了一个很好的“你好,世界!”正如预期的那样。 但是我现在想在我的页面中添加一个模板,因此我添加了几行:
this_file = "venv/bin/activate_this.py"
exec(open(this_file).read(), {'__file__': this_file})
from flask import Flask, render_template
application = Flask(__name__)
@application.route('/')
@application.route('/index')
def index():
return render_template('index.html')
我已经导入了 render_template 并从“return 'Hello'”更改为“return render_template('index.html')”。
很遗憾,我的网站现在显示“从应用程序收到不完整的响应”。我尝试了这两种结构:
www/
venv/
myapp.py
templates/
index.html
和:
www/
templates/
index.html
venv/
myapp.py
但它并没有改变任何东西......有谁知道如何返回模板?
【问题讨论】:
-
在 cmd 中激活您的环境,然后在那里运行您的应用程序,不要使用
exec,尤其是在不需要它的情况下,我很确定它不需要