烧瓶不渲染html

发布于 2025-02-08 16:51:02 字数 291 浏览 1 评论 0 原文

这是读取的代码,但烧瓶不渲染html

 from flask import Flask,render_template
 import psycopg2
 import psycopg2.extras

 app=Flask(__name__)

 app.route('/')
 def index():
      return render_template('index.html')

 if __name__=="__main__":
     app.run(debug=True, port=9001)

This is the code read but Flask not rendering html

 from flask import Flask,render_template
 import psycopg2
 import psycopg2.extras

 app=Flask(__name__)

 app.route('/')
 def index():
      return render_template('index.html')

 if __name__=="__main__":
     app.run(debug=True, port=9001)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔少女心 2025-02-15 16:51:02

如果我没有误解,那只是错别字错误。假设您已经在模板文件夹中拥有一个名为 index.html 的文件。因此,您的树看起来像这样:

.
└── my_app/
    ├── templates/
    │   └── index.html
    └── run.py

此代码应该有效:

from flask import Flask, render_template

app=Flask(__name__)

@app.route('/') # <-- You are missing the '@' here
def index():
    return render_template('index.html')

if __name__ == "__main__": # <-- Note this too
    app.run(debug=True, port=9001)

现在您可以通过

要了解有关烧瓶视图装饰的更多信息,请

有关有关的更多信息,如果__name __ ==“ __ main __”:,您可以阅读更多此处

If I'm not mistaking, it's just a typo error. Supposed you already have a file called index.html in your templates folder. So your tree look like this:

.
└── my_app/
    ├── templates/
    │   └── index.html
    └── run.py

This code should work:

from flask import Flask, render_template

app=Flask(__name__)

@app.route('/') # <-- You are missing the '@' here
def index():
    return render_template('index.html')

if __name__ == "__main__": # <-- Note this too
    app.run(debug=True, port=9001)

Now you can access to your project at http://127.0.0.1:9001/

To learn more about Flask view decorator, go here.

For more information about what mean if __name__ == "__main__":, you can read more here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文