Flask的渲染函数顺序?

huangapple go评论97阅读模式
英文:

Flask's order of rendering functions?

问题

  1. 我正在尝试建立一个基本的网站来练习Flask并在我的`app.py`文件中包含以下内容
  2. ```Python
  3. from flask import Flask, render_template
  4. app = Flask(__name__)
  5. @app.route("/")
  6. def hello_world():
  7. return "Hello, world!"
  8. @app.route('/')
  9. def home():
  10. return render_template('home.html')
  11. if __name__ == "__main__":
  12. app.run(
  13. host="0.0.0.0",
  14. debug=True
  15. )

home.html的内容如下:

  1. <div id="menubar">
  2. <ul>
  3. <li><a href="/">Home</a></li>
  4. </ul>
  5. </div>

我注意到当我运行python app.py时,“Hello, world!”被打印到屏幕上,但不显示home.html的内容。当我注释掉hello_world函数时,home.html的内容就被渲染出来了。

为什么会这样?我是否不了解某种渲染顺序?

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m trying to set up a basic website to practice Flask and have the following in my `app.py` file:
  4. ```Python
  5. from flask import Flask, render_template
  6. app = Flask(__name__)
  7. @app.route(&quot;/&quot;)
  8. def hello_world():
  9. return &quot;Hello, world!&quot;
  10. @app.route(&#39;/&#39;)
  11. def home():
  12. return render_template(&#39;home.html&#39;)
  13. if __name__ == &quot;__main__&quot;:
  14. app.run(
  15. host=&quot;0.0.0.0&quot;,
  16. debug=True
  17. )

The contents of home.html are:

  1. &lt;div id=&quot;menubar&quot;&gt;
  2. &lt;ul&gt;
  3. &lt;li&gt;&lt;a href=&quot;/&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
  4. &lt;/ul&gt;
  5. &lt;/div&gt;

I noticed that when I run the app as python app.py, Hello, world! is printed onto the screen but not home.html. When I comment out the hello_world function, then the contents of home.html are rendered.

Why is this? Is there some sort of rendering order that I'm not aware of?

答案1

得分: 2

每个路由都必须在@app.route()装饰器中定义,函数名称可以是您想要的任何名称。如果您想要两个不同的页面,您的app.py可能如下所示:

  1. from flask import Flask, render_template
  2. app = Flask(__name__)
  3. @app.route('/hello_world') # 这是 http://0.0.0.0:5000/hello_world
  4. def hello_world():
  5. return "Hello, world!"
  6. @app.route('/') # 这是 http://0.0.0.0:5000
  7. def home():
  8. return render_template('home.html')
  9. if __name__ == "__main__":
  10. app.run(host="0.0.0.0", debug=True)

如果您想将字符串Hello, world传递给home.html,您的app.py可能如下所示:

  1. from flask import Flask, render_template
  2. app = Flask(__name__)
  3. @app.route('/') # 这是 http://0.0.0.0:5000
  4. def home():
  5. msg = "Hello, world!"
  6. return render_template('home.html', msg=msg)
  7. if __name__ == "__main__":
  8. app.run(host="0.0.0.0", debug=True)

而您的home.html可能如下所示:

  1. <div id="menubar">
  2. <ul>
  3. <li><a href="/">Home</a></li>
  4. </ul>
  5. </div>
  6. <h1>{{ msg }}</h1>
英文:

Each route must be defined in the @app.route() decorator, the function names can be whatever you want them to be. If you want two different pages, your app.py could look like this:

  1. from flask import Flask, render_template
  2. app = Flask(__name__)
  3. @app.route(&#39;/hello_world&#39;) # this is http://0.0.0.0:5000/hello_world
  4. def hello_world():
  5. return &quot;Hello, world!&quot;
  6. @app.route(&#39;/&#39;) # this is http://0.0.0.0:5000
  7. def home():
  8. return render_template(&#39;home.html&#39;)
  9. if __name__ == &quot;__main__&quot;:
  10. app.run(host=&quot;0.0.0.0&quot;, debug=True)

If you want to pass the string Hello, world to home.html, your app.py could look like this:

  1. from flask import Flask, render_template
  2. app = Flask(__name__)
  3. @app.route(&#39;/&#39;) # this is http://0.0.0.0:5000
  4. def home():
  5. msg = &quot;Hello, world!&quot;
  6. return render_template(&#39;home.html&#39;, msg=msg)
  7. if __name__ == &quot;__main__&quot;:
  8. app.run(host=&quot;0.0.0.0&quot;, debug=True)

And your home.html could look like this:

  1. &lt;div id=&quot;menubar&quot;&gt;
  2. &lt;ul&gt;
  3. &lt;li&gt;&lt;a href=&quot;/&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
  4. &lt;/ul&gt;
  5. &lt;/div&gt;
  6. &lt;h1&gt;{{ msg }}&lt;/h1&gt;

huangapple
  • 本文由 发表于 2023年5月29日 09:25:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定