登录之后更新导航
- 用上下文处理器app_context_processor定义函数
- 获取session中保存的值
- 返回字典
- 在父模板中更新导航,插入登录状态判断代码。
- 注意用{% ... %}表示指令。
- {{ }}表示变量
- 完成注销功能。
- 清除session
- 跳转
主py文件:
from flask import Flask,request,render_template,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy import config app = Flask(__name__) app.config.from_object(config) db = SQLAlchemy(app) class User(db.Model): __tablename__='user' id = db.Column(db.Integer,primary_key=TabError,autoincrement=True) username = db.Column(db.String(20),nullable=False) password = db.Column(db.String(20), nullable=False) #db.create_all() # #增加 # user = User(username='123456',password = '456789') # db.session.add(user) # db.session.commit() # # #查询 # user =User.query.filter(User.username =='abcdef').first() # print(user.username,user.password) # # #修改 # user =User.query.filter(User.username =='123456').first() # user.password='abcdef' # print(user.username,user.password) # db.session.commit() # # #删除 # user =User.query.filter(User.username =='123456').first() # print(user.username,user.password) # db.session.delete(user) # db.session.commit() @app.route('/') def dh(): return render_template('dh.html') @app.route('/sy/') def sy(): return render_template('sy.html') @app.route('/dl/',methods=['GET','POST']) def dl(): if request.method == 'GET': return render_template("dl.html") else: usern = request.form.get('name') password = request.form.get('password') user = User.query.filter(User.username == usern).first() if user: if user.password == password: session['user'] = usern return redirect(url_for('dh')) else: return '密码错误(〃>皿<)' else: return '用户不存在┌(。Д。)┐' @app.context_processor def mycontext(): usern=session.get('user') if usern: return {'username':usern} else: return{} @app.route('/zc/',methods=['GET','POST']) def zc(): if request.method == 'GET': return render_template("zc.html") else: usern = request.form.get('name') password = request.form.get('password') user = User.query.filter(User.username == usern).first() if user: return '用户名已存在┗|*`0′*|┛ ' else: user1 = User(username=usern, password=password) db.session.add(user1) db.session.commit() return redirect(url_for('dl')) @app.route('/tc/') def tc(): session.clear(); return redirect(url_for('sy')) @app.route('/fk/') def fk(): return render_template('fk.html') if __name__ == '__main__': app.run(debug=True)
父模板:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> {% block title %} {% endblock %} 导航页</title> <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/dh.css') }}"> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <script src="{{ url_for('static',filename='js/dh.js') }}"></script> </head> {% block head %} {% endblock %} <nav class="navbar navbar-default" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="http://127.0.0.1:5000/"><img class="tubiao" id="tubiao" src="../static/image/movie.jpg"></a> </div> <form class="navbar-form navbar-left" role="search"> <div class="form-group"> <input type="text" class="form-control" placeholder="✍输入您感兴趣的"> </div> <button type="submit" class="btn btn-default">查找</button> <button type="button" class="btn btn-default navbar-btn">导航栏</button> </form> <ul class="nav navbar-nav navbar-right" id="myBody"> <li ><img class="tupian" id="myOnOff" onclick="myswitch()" src="../static/image/sun.png"></li> <li><a href="{{ url_for('sy') }}"><span class="glyphicon .glyphicon-home"></span> 首页</a></li> {% if username %} <li><a href="#">{{ username }}</a></li> <li><a href='{{ url_for('tc') }}'>退出</a></li> {% else %} <li><a href="{{ url_for('zc') }}"><span class="glyphicon glyphicon-user"></span> 注册</a></li> <li><a href="{{ url_for('dl') }}"><span class="glyphicon glyphicon-log-in"></span> 登录</a></li> {% endif %} <li><a h ref="{{ url_for('fk') }}"><span class="glyphicon .glyphicon-pencil"></span> 反馈</a></li> </ul> </div> </nav> <body id="myBody"> {% block main %} <p><span style="background-color: black;font-family: '方正舒体', Helvetica, Arial, sans-serif;font-size: 50px;color:red;align:center">☝☝请使劲戳顶部导航栏或底部导航栏☟☟</span></p> {% endblock %} <div class="dibu"> <hr align=center width=100% color=#c0c0c0 SIZE=1> <nav> <a class="daohang" href="http://www.360kan.com/dianying/index.html">360影视</a> <a class="daohang" href="http://www.youku.com/">优酷</a> <a class="daohang" href="http://v.qq.com/?ptag=360.kuzhan">腾讯视频</a> <a class="daohang" href="https://www.mgtv.com/">芒果TV</a> <a class="daohang" href="http://www.iqiyi.com/?vfm=f_268_360d&fv=07668579fcb3b76994a863f602b0dce3">爱奇艺</a> </nav> <p>版权@silky-L</p> </div> </body> </html>