flask简单应用(4)

一、初始化配置
二、app实例的配置
三、before_request、after_request、errorhandler三个装饰器
四、blueprint蓝图
五、蓝图示例
六、CBV版视图函数
七、WTForms
八、SQLAlchemy in Flask

一、flask_demo1 初始化配置

from flask import Flask, render_template

app = Flask(__name__, template_folder='template', static_folder='static_file', static_url_path='/static')

@app.route('/')
def index():
    return render_template('index.html')

if __name__ == '__main__':
    app.run()

# template_folder 指定模板文件的寻找名称
# static_url_path='/static' ,(必须加上斜杠)一般不用改,当前端页面大量使用原文件路径,
# 但是新文件路径改变了之后,就可以写上这个,确保html页面中的路径不用改动
# 不设置,默认是static_folder的名字

# static_folder 这个是告诉服务器,静态文件的存放地址
# static_url_path 这个是前端通过浏览器访问服务器的静态文件时的地址,
# 设置了这个意思就是:
#     我服务器看到这个路径就知道你要访问我的静态文件,然后我再从我存放静态文件的文件夹中去取,我存放文件的文件夹就是static_folder

二、flask_demo2 app实例的配置

app.py

from flask import Flask,session
import flask_config
app = Flask(__name__)

app.config['DEBUG'] = True  # 方式1
app.config.from_object(flask_config.Config)  # 方式2

@app.route('/')
def index():
    session['user'] = 'user'  # 验证方式二可不可以配置上
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

flask_config.py

class Config(object):  
    DEBUG = True
    SECRET_KEY = '123456789'
    REDIS = '127.0.0.1:6379'
    MYSQL = '127.0.0.1:3306'


if __name__ == '__main__':
    from flask import Flask
    app = Flask(__name__)

    for key in dir(Config):
        if key.isupper():
            print(key,type(key))
            app.config[key] = getattr(Config,key)
    # 和 app.config['DEBUG'] = True 一样的

三、flask_demo3 三个装饰器

from flask import Flask, render_template
app = Flask(__name__)

@app.before_request
def fumc1():
    print('before1')
    # return 'before1结束'

@app.before_request
def fumc2():
    print('before2')

@app.before_request
def fumc3():
    print('before3')

@app.after_request
def fumc4(res):
    print('after1')
    return res

@app.after_request
def fumc5(res):
    print('after2')
    return res

@app.after_request
def fumc6(res):
    print('after3')
    return res

@app.route('/')
def index():
    print('index')
    return 'Hello World!'


@app.errorhandler(404)
def error(e):
    print('走了error')
    print(e.name)
    # Not Found
    print(e.get_description())
    # 

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

return render_template('error.html') if __name__ == '__main__': app.run() # 类似于django的中间件 # @app.after_request必须接收参数,且返回一个响应 # 执行顺序: # before1 # before2 # before3 # index # after3 # after2 # after1 # 如果函数在中途被返回(打开1的return),则执行顺序为: # before1 # after3 # after2 # after1 # @app.errorhandler(404)函数中必须接收一个参数,否则报错。

所有函数都加装饰器

from flask import Flask,session,request,redirect,render_template
app = Flask(__name__)
app.secret_key = 'ASDFGHJKL'

# 写装饰器(一句话搞定)
@app.before_request
def check_login():
    if session.get('user') or request.path == '/login/':
        return None
    next = request.path
    return redirect('/login/?next=%s' % next)

@app.route('/index')
def index():
    return '这是index页面'

@app.route('/home')
def home():
    return '这是home页面'

@app.route('/login/',methods=('GET','POST'))
def login():
    msg = ''
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        if username == 'zhangsan' and password == '123':
            session['user'] = username
            next = request.args.get('next') or '/index'
            return redirect(next)
        else:
            msg = '用户名或密码错误'
    return render_template('login.html',msg=msg)

app.run()
# 这样做的不好之处是,如果访问本服务器没有的url也会让其登录,登录之后才告知没有该url。蓝图可解决只给蓝图内的函数加认证。

四、flask_demo4 blueprint蓝图

app.py

import flask_config
import flask_lantu1
import flask_lantu2
from flask import Flask

app = Flask(__name__)

app.config.from_object(flask_config.Config)

app.register_blueprint(flask_lantu1.addstu)
app.register_blueprint(flask_lantu2.delstu)

@app.route('/')
def index():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()
# 插拔机制
# 定义蓝图、引入蓝图、注册蓝图

flask_lantu1.py

from flask import Blueprint,render_template

addstu = Blueprint("addstu", __name__,
                   template_folder="blueprint_temp",
                   static_folder="blueprint_static",
                   static_url_path="/static2")

@addstu.route("/add_stu")
def add():
    return render_template("add_stu.html")

flask_lantu2.py

from flask import Blueprint

delstu = Blueprint("delstu", __name__)

@delstu.route("/del_stu")
def deletestu():
    return "del_stu_blueprint"

blueprint_temp/add_stu.html




    
    Title



1 2 3
图片:blueprint_static/f666.jpg

五、蓝图示例

before_request版登录验证蓝图.py

from flask import Blueprint,request,session,redirect
bp = Blueprint('bp',__name__)

@bp.before_request
def check_login():
    if session.get('user') or request.path == '/login/':
        return None
    next = request.path
    print('bp',next)
    return redirect('/login/?next=%s' % next)

@bp.route('/bp1')
def bp1():
    return 'This is 蓝图1'

@bp.route('/bp2')
def bp2():
    return '这是蓝图2'
# 单独在蓝图中设置before_request,可以实现只访问蓝图中的url需要验证,如果输入的url不存在可立即提示不存在。

app.py

from flask import Flask,redirect,request,render_template,session
import before_request版登录验证蓝图

app = Flask(__name__)
app.secret_key='ASDF'

app.register_blueprint(before_request版登录验证蓝图.bp)
# session['user'] = 'username'  # session放在有效的函数中才可使用,否则报错,因为不符合请求上下文。
@app.route('/')
def index():
    return 'Hello World!'

@app.route('/home')
def home():
    return 'Welcome to Home!'

@app.route('/login/',methods=('GET','POST'))
def login():
    msg = ''
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        if username == 'zhangsan' and password == '123':
            session['user'] = username
            next = request.args.get('next') or '/index'
            print('flask',next)
            return redirect(next)
        else:
            msg = '用户名或密码错误'
    return render_template('login.html',msg=msg)

if __name__ == '__main__':
    app.run()

templates/login.html




    
    Title


用户名:
密码:

{{ msg }}

六、flask_demo6 CBV版视图函数

app.py

from flask import Flask, views
import flask_demo2蓝图中的CBV
app = Flask(__name__)

app.register_blueprint(flask_demo2蓝图中的CBV.bp)

class Index(views.MethodView):
    # methods = ['POST','GET']
    def get(self):
        # self.dispatch_request()
        return 'GET'
    def post(self):
        return 'POST'

app.add_url_rule('/index', endpoint='class_index', view_func=Index.as_view(name='index'))

if __name__ == '__main__':
    app.run()

flask_demo2蓝图中的CBV.py

from flask import Blueprint, views
bp = Blueprint("bluename", __name__)

class bpclass(views.MethodView):
    def get(self):
        return "blue_get"

bp.add_url_rule("/blue",endpoint="blue",view_func=bpclass.as_view(name="bpclass"))
flask简单应用(4)_第1张图片
CBV版.png

七、flask_demo7 之 WTForms

(pip install wtforms)

from flask import Flask, views, render_template, request, session

app = Flask(__name__)

from wtforms.fields import simple, core
from wtforms import Form
from wtforms import validators

class LoginForm(Form):
    username = simple.StringField(
        label="用户名",
        validators=[
            validators.DataRequired(message="数据不能为空"),
            validators.Length(min=5, max=16, message="大于5小于16")
        ],
        render_kw={"class": "1班"}
    )

    password = simple.PasswordField(
        label="密码",
        validators=[
            validators.DataRequired(message="数据不能为空"),
            validators.Length(min=5, max=16, message="大于5小于16"),
            validators.Regexp(regex="\d+", message="密码必须为数字")
        ],
    )


class Index(views.MethodView):
    def get(self):
        loginfm = LoginForm()
        return render_template("index.html", fm=loginfm)

    def post(self):
        loginfm = LoginForm(request.form)
        if not loginfm.validate():
            return render_template("index.html", fm=loginfm)
        session["user"] = "good!"
        return "Hello"


app.add_url_rule("/index", endpoint="class_index", view_func=Index.as_view(name="123"))


class RegForm(Form):
    hobby_init = []

    # def __init__(self, hobby_list):
    #     self.hobby_list = hobby_list
    #     # self.hobby_init = []
    #     self.init_hobby()
    #     super(RegForm, self).__init__()

    # def init_hobby(self):
    #     for index, item in enumerate(self.hobby_list):
    #         self.hobby_init.append([index, item])

    username = simple.StringField(
        label="用户名",
        validators=[
            # validators.DataRequired(message="数据不能为空"),
            validators.Length(min=5, max=16, message="大于5小于16")
        ],
        render_kw={"class": "username"}
    )

    password = simple.PasswordField(
        label="密码",
        validators=[
            validators.DataRequired(message="数据不能为空"),
            validators.Length(min=5, max=16, message="大于5小于16"),
            validators.Regexp(regex="\d+", message="密码必须为数字")
        ],
    )

    repassword = simple.PasswordField(
        label="密码",
        validators=[
            # validators.DataRequired(message="数据不能为空"),
            validators.EqualTo("password", message="两次密码不一致")
        ],
    )

    gender = core.RadioField(
        label="性别",
        choices=(
            (1, "女"),
            (2, "男"),
            (3, "保密")
        ),
        coerce=int,
        default=3
    )

    hobby = core.SelectMultipleField(
        label="爱好",
        choices=[[1, "cy"]],
        coerce=int,
        default=1
    )


@app.route("/reg", methods=["GET", "POST"])
def reg():
    if request.method == "GET":
        # hobby_list = ["cy","hj","bd","ysd","啤酒泡枸杞","熬夜敷面膜"]
        regfm = RegForm()
        return render_template("reg.html", fm=regfm)
    else:
        regfm = RegForm(request.form)
        if not regfm.validate():
            return render_template("reg.html", fm=regfm)

        print(regfm.data.get("hobby"), regfm.data.get("gender"))

        return "注册成功"

if __name__ == '__main__':
    app.run()

templates/index.html




    
    Title


    请登录:
    

{{ fm.username.label }} {{ fm.username }} {{ fm.username.errors.0 }}

{{ fm.password.label }} {{ fm.password }} {{ fm.password.errors.0 }}

templates/reg.html




    
    Title


    
{% for field in fm %}

{{ field.label }} {{ field }} {{ field.errors.0 }}

{% endfor %}

你可能感兴趣的:(flask简单应用(4))