登录之后更新导航
- 用上下文处理器app_context_processor定义函数
- 获取session中保存的值
- 返回字典
from flask import Flask, render_template, url_for, redirect, request,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=True, autoincrement=True) username = db.Column(db.String(20), nullable=False) password = db.Column(db.String(20), nullable=False) nickname = db.Column(db.String(20)) db.create_all() # 增加 # user = User(username='tan1997',password='19961021') # db.session.add(user) # db.session.commit() # 查询 # user = User.query.filter(User.username == 'tan1997').first() # print(user.username,user.password) # 修改 # user=User.query.filter(User.username == 'tan1997').first() # user.password=1234567 # db.session.commit() # 删除 # user=User.query.filter(User.username == 'tan1997').first() # db.session.delete(user) # db.session.commit() @app.route('/') def myweb(): return render_template("myweb.html") @app.route('/login/', methods=['GET', 'POST']) def login(): if request.method == 'GET': return render_template("login.html") else: username = request.form.get('username') password = request.form.get('password') user = User.query.filter(User.username == username).first() if user: if user.password == password: session['user'] = username session.permannet = True return redirect(url_for('myweb')) else: return '密码错误' else: return '用户名不存在' @app.context_processor def mycontext(): usern = session.get('user') if usern: return {'username':usern} else: return {} @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 ' 用户名已存在' else: user = User(username=username, password=password, nickname=nickname) db.session.add(user) # 数据库,添加操作 db.session.commit() return redirect(url_for('login')) @app.route('/question/') def question(): return render_template("question.html") if __name__ == '__main__': app.run(debug=True)
- 在父模板中更新导航,插入登录状态判断代码。、
- 注意用{% ... %}表示指令。
- {{ }}表示变量
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <title>主页 {% block logintitle %} {% endblock %} {% block registtitle %} {% endblock %} {% block questiontitle %} {% endblock %} </title> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/myweb.css') }}"> <script src="{{ url_for('static',filename='js/switch.js') }}"></script> {% block loginhead %} {% endblock %} {% block registhead %} {% endblock %} {% block questionhead %} {% endblock %} </head> <body id="myBody" style="background-image: url(../static/img/demo-1-bg.jpg)"> <a> <li><img class="ico" src="../static/img/ico.jpg" alt="" style="margin-top: 12px"></li> <li><a href="{{ url_for('myweb') }}">首页</a></li> <li><input type="text" class="form-control" placeholder="Search" style="margin-top: 8px" ></li> <li><button type="submit" class="btn btn-default" style="margin-top: 8px">搜索</button></li> <li><a href="{{ url_for('question') }}">提问</a></li> <p>{{ username }}AAAA</p> {% if username %} <li><a href="#">{{ username }}</a></li> <li><a href="#">注销</a></li> {% else %} <li style="float:right"><a href="{{ url_for('login') }}">登陆</a></li> <li style="float:right"><a href="{{ url_for('regist') }}">注册</a></li> {% endif %} <li style="float: right"> <img id="myOnOff" onclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" class="bulb"></li> </ul> <footer> <div class="footer_box"> <p>Posted by: W3School</p> <p>Contact information: <a href="mailto:someone@example.com">someone@example.com</a>.</p> </div> </footer> {% block loginbody %} {% endblock %} {% block registbody %} {% endblock %} {% block questiontbody %} {% endblock %} </body> </html>
- 完成注销功能。
- 清除session
- 跳转
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <title>主页 {% block logintitle %} {% endblock %} {% block registtitle %} {% endblock %} {% block questiontitle %} {% endblock %} </title> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/myweb.css') }}"> <script src="{{ url_for('static',filename='js/switch.js') }}"></script> {% block loginhead %} {% endblock %} {% block registhead %} {% endblock %} {% block questionhead %} {% endblock %} </head> <body id="myBody" style="background-image: url(../static/img/demo-1-bg.jpg)"> <a> <li><img class="ico" src="../static/img/ico.jpg" alt="" style="margin-top: 12px"></li> <li><a href="{{ url_for('myweb') }}">首页</a></li> <li><input type="text" class="form-control" placeholder="Search" style="margin-top: 8px" ></li> <li><button type="submit" class="btn btn-default" style="margin-top: 8px">搜索</button></li> <li><a href="{{ url_for('question') }}">提问</a></li> <p>{{ username }}AAAA</p> {% if username %} <li><a href="#">{{ username }}</a></li> <li><a href="#">注销</a></li> {% else %} <li style="float:right"><a href="{{ url_for('login') }}">登陆</a></li> <li style="float:right"><a href="{{ url_for('regist') }}">注册</a></li> {% endif %} <li style="float: right"> <img id="myOnOff" onclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" class="bulb"></li> </ul> <footer> <div class="footer_box"> <p>Posted by: W3School</p> <p>Contact information: <a href="mailto:someone@example.com">someone@example.com</a>.</p> </div> </footer> {% block loginbody %} {% endblock %} {% block registbody %} {% endblock %} {% block questiontbody %} {% endblock %} </body> </html>