传递参数的路由
@app.route('/')
def hello_world():
content = "hellowrld"
return render_template("index.html", content=content)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>{{content}}</h1>
</body>
</html>
创建类
单独类文件
class User(object):
def __init__(self, user_id , user_name):
self.user_id =user_id
self.user_name = user_name
路由部分
from models import User
@app.route('/user')
def user_index():
user = User(1, 'jikexueyuan')
return render_template("user_index.html", user=user)
带选择的路由部分
@app.route('/query_user/<user_id>')
def query_user(user_id):
user = None
if int(user_id) == 1:
user=User(1, 'jikexueyuan')
return render_template("user_index.html",user=user)
网页部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% if user %}
hello {{ user.user_name }}
{% else %}
no user
{% endif %}
</body>
</html>
循环
@app.route('/users')
def user_list():
users = []
for i in range(1,11):
user = User(i, 'jikexueyuan '+str(i))
users.append(user)
return render_template("user_list.html", users=users)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for user in users %}
{{ user.user_id }}--{{ user.user_name }}<br>
{% endfor %}
</body>
</html>
继承
@app.route('/one')
def one_base():
return render_template("one_base.html")
@app.route('/two')
def two_base():
return render_template("two_base.html")
父
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<h1>header 头部</h1>
</div>
{% block content %}
{% endblock %}
<div>
<h1>footer 尾部</h1>
</div>
</body>
</html>
子1
{% extends "base.html" %}
{% block content %}
<h2>第一页</h2>
{% endblock %}
子2
{% extends "base.html" %}
{% block content %}
<h2>第二页</h2>
{% endblock %}