【问题标题】:Flasks - render_template not rendering the html pageFlasks - render_template 不渲染 html 页面
【发布时间】:2019-12-25 17:56:07
【问题描述】:

我需要以下代码的帮助。

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 Internal Server Error。 有人可以帮助代码有什么问题吗? 上述文件 (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 行 应用程序运行() ^ IndentationError: 需要一个缩进块

【问题讨论】:

  • 我认为你需要检查 Apache 错误日志
  • 烧瓶日志说什么?
  • @CrazyElf - 使用 apache 错误日志编辑了代码
  • if __name__ == "__main__" 块在hello_world() 函数定义下缩进。不应该。
  • @CrazyElf & marxmacher - 非常感谢大家的帮助。我检查了 apache 错误日志,发现这是一个缩进相关的问题。我已经修复它并且代码按预期工作

标签: python flask


【解决方案1】:

File "/var/www/html/flaskapp/flaskapp.py", line 8 app.run() ^ IndentationError: expected an indented 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多