登录之后更新导航
- 用上下文处理器app_context_processor定义函数
- 获取session中保存的值
- 返回字典
- 在父模板中更新导航,插入登录状态判断代码。、
- 注意用{% ... %}表示指令。
- {{ }}表示变量
- 完成注销功能。
- 清除session
- 跳转
from flask import Flask,render_template,request,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy import config from functools import wraps from datetime import datetime app=Flask(__name__) app.config.from_object(config) db=SQLAlchemy(app) class User(db.Model): __tablename__='user' id=db.Column(db.Integer, primary_key=True,autoincrement=True) username = db.Column(db.String(20), nullable=False) password = db.Column(db.String(20), nullable=False) nickname = db.Column(db.String(50)) # db.create_all() # 添加/删除 # user=User.query.filter(User.username=='mis1',password='mis1') # db.session.add/delete(user) # db.session.commit() # 查询 # user=User.query.filter(User.username =='mis1').first() # print(user.username,user.password) # 修改 # user=User.query.filter(User.username=='mis1').first() # user.password='123456' # user.username='mis1' # print(user.username,user.password) # db.session.commit() @app.route('/') def base(): return render_template('base.html') @app.route('/regist/',methods=['GET','POST']) def regist(): if request.method=='GET': return render_template('regist.html') else: username=request.form.get('username') password=request.form.get('password') nickname = request.form.get('nickname') user = User.query.filter(User.username==username).first() if user: return 'username existed' else: user=User(username=username,password=password,nickname=nickname) db.seesion.add(user)#数据库操作 db.seesion.commit() return redirect(url_for('login')) @app.route('/login/',methods=['GET','POST']) def login(): if request.method=='GET': return render_template('login.html') else: username2=request.form.get('username') password2=request.form.get('password') user = User.query.filter(User.username == username2,User.password==password2).first() if user: session['user'] == username2 session.permanent = True return render_template(url_for('base')) else: return u'error username or password' # if user.password==password2: # return redirect(url_for('base')) # else: # return u'password error' # else: # return u'username is not existed.' @app.context_processor def mycontext(): username2=session.get('user') if username2: return {'username':username2} else: return {} @app.route('/ask/') def ask(): return render_template('ask.html') @app.route('/logout/') def logout(): session.clear() return redirect(url_for(base)) if __name__=='__main__': app.run()
<!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/base.css') }}"> <script src="{{ url_for('static',filename='js/base.js') }}"></script> {% block head %} {% endblock %} </head> <body id="myBody"> <nav> <img src="http://p1.img.cctvpic.com/photoAlbum/page/performance/img/2013/8/27/1377583188891_732.jpg" width="800px"height="150px"><br> <img id="myOnOff" onclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" width="25px"> <p>{{ username }}contextx</p> <a href="http://www.sesamestreetchina.com.cn/">首页</a> <input type="text"name="search"> <button type="submit">搜索</button> <a href="{{ url_for('ask') }}">发布问题</a> {% if username %} <li><a href="#"{{username}}></a></li> <li><a href="{{url_for('logout')}}">注销</a>></li> {% else %} <li><a href="{{ url_for('login') }}">登录</a></li> <li><a href="{{ url_for('regist') }}">注册</a></li> {% endif %} <img src="{{ url_for('static',filename='image/86x97sZ11K4.jpg') }}" alt="" width="50px"> </nav> {% block main %} {% endblock %} <div class="area"> </div> <footer> <div class="footer_box"> Copyright@2017 个人版权,版权所有 作者:杨碧茜</div> </footer> </body> </html>