英文:
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'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("/")
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
)
The contents of home.html
are:
<div id="menubar">
<ul>
<li><a href="/">Home</a></li>
</ul>
</div>
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('/hello_world') # this is http://0.0.0.0:5000/hello_world
def hello_world():
return "Hello, world!"
@app.route('/') # this is 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)
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('/') # this is 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)
And your home.html
could look like this:
<div id="menubar">
<ul>
<li><a href="/">Home</a></li>
</ul>
</div>
<h1>{{ msg }}</h1>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论