Flask的渲染函数顺序?

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

Flask's order of rendering functions?

问题

我正在尝试建立一个基本的网站来练习Flask并在我的`app.py`文件中包含以下内容

```Python
from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "Hello, world!"

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

if __name__ == "__main__":
    app.run(
        host="0.0.0.0",
        debug=True
    )

home.html的内容如下:

<div id="menubar">
    <ul>
        <li><a href="/">Home</a></li>
    </ul>
</div>

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

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


<details>
<summary>英文:</summary>

I&#39;m trying to set up a basic website to practice Flask and have the following in my `app.py` file:

```Python
from flask import Flask, render_template


app = Flask(__name__)


@app.route(&quot;/&quot;)
def hello_world():
    return &quot;Hello, world!&quot;


@app.route(&#39;/&#39;)
def home():
    return render_template(&#39;home.html&#39;)


if __name__ == &quot;__main__&quot;:
    app.run(
        host=&quot;0.0.0.0&quot;,
        debug=True
    )

The contents of home.html are:

&lt;div id=&quot;menubar&quot;&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;a href=&quot;/&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&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可能如下所示:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/hello_world')  # 这是 http://0.0.0.0:5000/hello_world
def hello_world():
    return "Hello, world!"

@app.route('/')  # 这是 http://0.0.0.0:5000
def home():
    return render_template('home.html')

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)

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

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')  # 这是 http://0.0.0.0:5000
def home():
    msg = "Hello, world!"
    return render_template('home.html', msg=msg)

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)

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

<div id="menubar">
    <ul>
        <li><a href="/">Home</a></li>
    </ul>
</div>
<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:

from flask import Flask, render_template

app = Flask(__name__)


@app.route(&#39;/hello_world&#39;)  # this is http://0.0.0.0:5000/hello_world
def hello_world():
    return &quot;Hello, world!&quot;


@app.route(&#39;/&#39;)  # this is http://0.0.0.0:5000
def home():
    return render_template(&#39;home.html&#39;)


if __name__ == &quot;__main__&quot;:
    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:

from flask import Flask, render_template

app = Flask(__name__)


@app.route(&#39;/&#39;)  # this is http://0.0.0.0:5000
def home():
    msg = &quot;Hello, world!&quot;
    return render_template(&#39;home.html&#39;, msg=msg)


if __name__ == &quot;__main__&quot;:
    app.run(host=&quot;0.0.0.0&quot;, debug=True)

And your home.html could look like this:

&lt;div id=&quot;menubar&quot;&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;a href=&quot;/&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;
&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:

确定