python - Flasks - render_template 不渲染 html 页面

标签 python flask

我需要有关以下代码的帮助。

root@ip-xxxxxxxx:~/flaskapp# cat flaskapp.py
from flask import Flask, render_template, redirect, url_for, request
app = Flask(__name__)

@app.route('/hello')
def hello_world():
    return render_template('testing.html', name = 'john')
    if __name__ == '__main__':
   app.run()
root@ip-xxxxxxx:~/flaskapp#

当我打开我的网站 ( http://www.example.com/hello ) 时,页面返回 500 内部服务器错误。 有人可以帮助代码有什么问题吗? 上述文件(flaskapp.py)位于html根目录下的flaskapp文件夹下。此外,testing.html 也放置在flaskapp 文件夹内的templates 文件夹下。

以下是/etc/apache2/sites-enabled/000-default.conf 文件的内容

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi

<Directory flaskapp>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>

下面是apache错误日志

文件“/var/www/html/flaskapp/flaskapp.py”,第 8 行 应用程序.run() ^ IndentationError:需要一个缩进 block

最佳答案

文件“/var/www/html/flaskapp/flaskapp.py”,第 8 行 app.run() ^ IndentationError:需要缩进 block

Apache 告诉您,由于缩进问题,读取 flaskapp.py 中的第 8 行时出现错误。 Python is very sensitive with regards to indentation.你有:

    if __name__ == '__main__':
   app.run()

希望这有帮助!

app.run() 应进一步缩进,使其看起来位于 if 语句的“下方”。尝试将您的代码更改为如下所示,看看是否可以解决问题:

    if __name__ == '__main__':
        app.run()

关于python - Flasks - render_template 不渲染 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59480738/

相关文章:

python - Django 表单中的只读字段

python - WSL 上没有名为tensorflow.python.platform 的模块

python - 如何在 Flask 中获取 HTTP 版本

python - 在flask-sqlalchemy中使用with_entities()返回自定义属性?

python - 如何从列表字段访问属性

python - Flask-SQLAlchemy 单元测试运行两次

python : Compare two large files

python - Pandas 日期时间列操作

python - 尝试循环裁剪图像时出现 TypeError : NoneType,

python - 从网络摄像头捕获图像并将其上传到 Flask 服务器