from flask import Flask
app = Flask(__name__)
@app.route('/blog/' )
def blog_detail(blog_id):
return "访问的是:%s"% blog_id
from flask import Flask , request
@app.route('/book/list')
def book_list():
page = request.args.get("page",default = 1,type = int)
return f"您获取的是第{page}的图书列表!"
http://127.0.0.1:5000/book/list?page=5
from flask import Flask,render_template
@app.route('/blog/' )
def blog_detail(blog_id):
return render_template("blog_detail.html",blog_id,username = "krien")
<body>
<p> 您的用户名是 {{ username }}p>
<h1>您访问的博客详情是:{{ blog_id }}h1>
<body>
class User:
def __init__(self,username,email):
self.username = username
self.email = email
@app.route('/')
def hello_world(): # put application's code here
user = User(username = "krien", email = "[email protected]")
return render_template('index.html',user = user)
<body>
{{ user.username }} / {{ user.email }}
<body>
from flask import Flask,render_template ,request
app = Flask(__name__)
@app.route('/')
def hello_world(): # put application's code here
user = User(username = "krien", email = "[email protected]")
person = {
"username":"张三",
"email":"[email protected]"
}
return render_template('index.html',user = user,person = person)
<body>
<div> {{ user.username }} / {{ user.email }} div>
<div> {{ person.username}} / {{ person.email }} div>
<body>
from flask import Flask,render_template ,request
from flask import Flask,render_template ,request
@app.route('/filter')
def filter_demo(): # put application's code here
user = User(username = "krien", email = "[email protected]")
return render_template('filter.html',user = user)
<body>
{{ user.username }}-{{ user.username|length }}
<body>
过滤器本质上是一个 Python 函数,他会把过滤的值当作第一个参数传给这个函数,函数经过一些逻辑处理后,在返回新的值。在过滤器函数写好之后,可以通过@app.template_filter装饰器或者是 app.add_template_filter 函数来把函数注册成Jinjia2 能用的过滤器
from flask import Flask,render_template ,request
from datetime import datetime
app = Flask(__name__)
def datetime_format(value,format = "%Y年%m月%d日 %H:%M"):
return value,strftime(format)
app.add_template_filter(datetime_format,"dformat")
@app.route('/filter')
def filter_demo(): # put application's code here
user = User(username = "krien", email = "[email protected]")
mytime = date_time.now()
return render_template('filter.html',user = user,mytime = mytime)
<body>
{{ user.username }}-{{ user.username|length }}
<div> {{mytime|dformat}} div>
<body>
@app.route("/control")
def control_statement():
age = 17
return render_template("control.html",age=age)
<body>
{{% if age>18 %}}
<div>您已经满18,可以进入网吧div>
{{% elif age==18 %}}
<div>您刚满18,需要父母陪同div>
{{% else %}}
<div>您未满18,不能进去div>
{{% endif %}}
<body>
{{% for book in books %}}
<div>图书名称:{{ book.name }},图书作者:{{ book.auther }}div>
{{% endfor %}}
from sqlalchemy import text
app = Flask(__name__)
HOSTNAME = 'localhost'
PORT = 3306
USERNAME = "root"
PASSWORD = "123456"
DATABASE = "database_learn"
app.config['SQLALCHEMY_DATABASE_URI'] = f"mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}/{DATABASE}"
db = SQLAlchemy(app)
with app.app_context():
with db.engine.connect() as conn:
rs = conn.execute(text("select 1"))
print(rs.fetchone())
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer,primary_key = True,autoincrement = True)
username = db.Column(db.String(100),nullable = False)
password = db.Column(db.String(100),nullable = False)
with app.app_context():
db.create_all()
@app.route('/user/add')
def add_user():
user = User(username="krien",password = '123456')
user1 = User(username='ddd',password = '121212')
db.session.add(user)
db.session.commit()
return "用户创建成功"
@app.route('/user/query')
def query_user():
# 1.get
user = User.query.get(1)
print(f"{user.id}:{user.username}-{user.password}")
# 2.filter_by 查找
return "数据查找成功"
@app.route('/user/update')
def update_user():
user = User.query.filter_by(username = "krien").first()
user.password = '222222'
db.session.commit()
return "数据更新成功"
@app.route('/user/delete')
def delete_user():
user = User.query.get(2)
db.session.delete(user)
db.session.commit()
return "数据删除成功"