This commit is contained in:
guanjihuan 2024-02-26 20:23:04 +08:00
parent 342efb8562
commit 5367038e59
4 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,10 @@
from flask import Flask
app = Flask(__name__) # 创建Flask应用程序实例。将__name__作为参数传递给 Flask 类的构造函数可以告诉Flask应用程序在哪里寻找静态文件夹、模板文件夹等相关资源。
@app.route('/') # 定义一个路由将根URL'/'与hello()函数关联起来
def hello():
return 'Hello World!'
if __name__ == '__main__': # 运行应用程序
app.run(debug=True) # 增加debug=True可以实现自动重载Flask会监视代码是否更改

View File

@ -0,0 +1,15 @@
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/about')
def about():
name = 'Guan'
return render_template('about.html', name=name)
if __name__ == '__main__':
app.run(debug=True)

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>About</title>
</head>
<body>
<h1>About Me</h1>
<p>This is the about page content. My name is {{name}}.</p>
<p><a href="/">Home</a></p>
<!-- Flask使用的是Jinja2模板引擎。Jinja2使用 {{ 和 }} 来标识变量/动态内容。 -->
</body>
</html>

View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<h1>Welcome to My Homepage!</h1>
<p><a href="/about">About</a></p>
</body>
</html>