• Flask基础-基础实例


    1. 10行代码的迷你程序 flask项目 

    from flask import  Flask
    
    app = Flask(__name__)
    
    
    @app.route("/index")
    def inde():
        return "heeello world"
    
    if __name__ =="__main__":
        app.run()
    

      

     2.页面登录的小程序+session简单用法.

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

    app = Flask(__name__)
    app.secret_key="dd"

    @app.route("/login",methods =["GET","POST"])
    def login():
    if request.method =='GET':

    return render_template("login.html")

    #request.form为post请求
    user =request.form.get("user")
    pwd =request.form.get("pwd")
    if user =="hailong" and pwd =="kuang":

    #设置session , flask的session存放在cookie当中
    session["user_infor"]=user
    print(22)
    return redirect("/index")
    print(111)
    return render_template("login.htm",msg ="用户密码错误")

    @app.route("/index")
    def index():

    #获取cookie 判断是否存在,如果存在并匹配进行跳转到相关页面
    user_infor =session.get("user_infor")
    print(222,user_infor)
    if not user_infor:
    return redirect("/login")
    return render_template("index.html")

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

    login页面

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>login</title>
    
    </head>
    </head>
    <body>
        <h1>     用户登录</h1>
    <form method="post">
        <input type="text" name ="user">
        <input type="password" name ="pwd">
        <input type="submit" value="提交">{{msg}}
    
    </form>
    </body>
    </html>

    index界面

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
            <h1>欢迎来到郝龙江家里</h1>
    </body>
    </html>

    3 .auth认证.

     1 from flask import Flask,render_template,request,redirect,session
     2 
     3 app = Flask(__name__)
     4 
     5 app.secret_key = '39jrlasdfoajslfu8af'
     6 
     7 
     8 import functools
     9 
    10 def auth(func):
    11     @functools.wraps(func) # 保留函数的元信息
    12     def inner(*args,**kwargs):
    13         if not session.get('user_info'):
    14             return redirect('/login')
    15         ret = func(*args,**kwargs)
    16         return ret
    17     return inner
    18 
    19 
    20 @app.route('/login',methods=['GET','POST'])
    21 def login():
    22     if request.method == 'GET':
    23         return render_template('login.html')
    24 
    25     user = request.form.get('user')
    26     pwd = request.form.get('pwd')
    27     if user == 'hailong' and pwd == 'kuang':
    28         session['user_info'] = user
    29         return redirect('/index')
    30     return render_template('login.html',msg="用户名或密码错误")
    31 
    32 
    33 @app.route('/index')
    34 @auth
    35 def index():
    36     kuang_list = [
    37         {'id':1,'name':'一矿','address':'大同'},
    38         {'id':2,'name':'二矿','address':'大同'},
    39         {'id':3,'name':'三矿','address':'大同'},
    40     ]
    41 
    42     return render_template('index.html',klist=kuang_list)
    43     # return render_template('index.html',**{'klist':kuang_list})
    44 
    45 @app.route('/detail')
    46 @auth
    47 def detail():
    48     nid = request.args.get('nid')
    49     return render_template('detail.html')
    50 
    51 
    52 @app.route('/logout')
    53 def logout():
    54     del session['user_info']
    55 
    56     return redirect('/login')
    57 
    58 if __name__ == '__main__':
    59     app.run()

     4.中间件 

     1 from flask import Flask,render_template,request,redirect,session
     2 
     3 app = Flask(__name__)
     4 
     5 app.secret_key = '39jrlasdfoajslfu8af'
     6 
     7 
     8 @app.before_request
     9 def xxxxxxxxxxxxxx():
    10     if request.path == '/login':
    11         return None
    12 
    13     if not session.get('user_info'):
    14         return redirect('/login')
    15 
    16 
    17 @app.route('/login',methods=['GET','POST'])
    18 def login():
    19     print('我是login')
    20     if request.method == 'GET':
    21         return render_template('login.html')
    22 
    23     user = request.form.get('user')
    24     pwd = request.form.get('pwd')
    25     if user == 'hailong' and pwd == 'kuang':
    26         session['user_info'] = user
    27         return redirect('/index')
    28     return render_template('login.html',msg="用户名或密码错误")
    29 
    30 
    31 @app.route('/index')
    32 def index():
    33     print('我是index')
    34     kuang_list = [
    35         {'id':1,'name':'一矿','address':'大同'},
    36         {'id':2,'name':'二矿','address':'大同'},
    37         {'id':3,'name':'三矿','address':'大同'},
    38     ]
    39 
    40     return render_template('index.html',klist=kuang_list)
    41     # return render_template('index.html',**{'klist':kuang_list})
    42 
    43 @app.route('/detail')
    44 def detail():
    45     nid = request.args.get('nid')
    46     return render_template('detail.html')
    47 
    48 
    49 @app.route('/logout')
    50 def logout():
    51     del session['user_info']
    52 
    53     return redirect('/login')
    54 
    55 if __name__ == '__main__':
    56     app.run()

     

  • 相关阅读:
    在windwos创建的脚本文件在linux环境中无法执行的问题
    shell的文件锁操作
    systemd target
    算法-排序数组
    算法-存在重复元素
    算法-移除元素
    算法-两数之和
    touch事件详解
    小程序 打包太大
    taro/vue 左滑删除购物车
  • 原文地址:https://www.cnblogs.com/mengbin0546/p/10261628.html
Copyright © 2020-2023  润新知