flask web开发jinja2
项目结构
核心app.python
from flask import Flask, render_template, request
from datetime import datetime
# render_template 就是渲染模板的的意思,也就是渲染模板的库
app = Flask(__name__)
# 自定义过滤器
def datatime_formate(value, formate="%Y年%m月%d日 %H:%M"):
return value.strftime(formate)
# 将函数datatime_formate别名为过滤器dformate
app.add_template_filter(datatime_formate, "dformate")
class User:
def __init__(self, username, email):
self.username = username
self.email = email
@app.route('/')
def hello_world(): # put application's code here
person = {
"username": "菜鸡学安全",
"email": "caiji@123.com"
}
user = User(username="菜鸡学安全", email="xxxxxx.123.com")
return render_template("index.html", user=user, person=person)
@app.route("/blog/<blog_id>")
def blog_detail(blog_id):
username = request.args.get("name", default="没有姓名", type=str)
return render_template("blog_detail.html", blog_id=blog_id, username=username)
# jinja2模板可以给前端传递参数
@app.route("/filter")
def filter_demo():
user = User(username="菜鸡xxxxx", email="xxxx@qq.com")
mytime =datetime.now()
return render_template("filter.html", user=user, mytime=mytime)
@app.route("/control")
def control_statemate():
age = 19
books = [{"name": "三国演义", "author": "罗贯中"},
{"name": "西游记", "author": "吴承恩"},
{"name": "红楼梦", "author": "曹雪芹"},
{"name": "水浒传", "author": "施耐庵"},
{"name": "沉沦", "author": "郁达夫"}]
return render_template("control.html", age=age, books=books)
# jinja2 模板for没有break
@app.route("/child1")
def child1():
return render_template("child1.html")
@app.route("/child2")
def child2():
return render_template("child2.html")
@app.route("/static")
def static_demo():
return render_template("static.html")
if __name__ == '__main__':
app.run()
# 参考链接 https://www.bilibili.com/video/BV17r4y1y7jJ
jinja2 控制语句
control.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>控制语句Demo</title>
</head>
<body>
{# 这里是jinja2的if语句怎么使用 #}
{% if age>18 %}
<div>您已经满18岁,可以进入网吧</div>
{% elif age==18 %}
<div>您刚满18岁,也可以进入网吧</div>
{% else %}
<div>您未满18岁不能进入网吧</div>
{% endif %}
{# 这里是循环 #}
{% for i in books %}
<div>图书名称: {{ i.name }} ----- 作者: {{ i.author }} </div>
{% endfor %}
</body>
</html>
模板继承
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<li><a href="#">首页</a></li>
<li><a href="#">新闻</a></li>
{% block body %}
{% endblock %}
<footer>我这里是底部</footer>
</body>
</html>
child1.html
{% extends "base.html" %}
{# 如果活动部分要怎么定义呢? 在base.html 中添加block #}
{% block title %}
我这里是子模板的标题
{% endblock %}
{% block body %}
我 是子模板的文件体
{% endblock %}
样例
引入css和js
style.css
body{
background-color: lightpink;
}
my.js
alert("我是my.js 文件")
static.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="{{ url_for('static', filename='/css/style.css') }}">
<script src="{{ url_for('static', filename='/js/my.js') }}"></script>
</head>
<body>
<img src="{{ url_for('static', filename='/images/test.jpg') }}" alt="">
</body>
</html>
样例
参考链接
https://www.bilibili.com/video/BV17r4y1y7jJ