登录之后更新导航
- 用上下文处理器app_context_processor定义函数
- 获取session中保存的值
- 返回字典
- 在父模板中更新导航,插入登录状态判断代码。
- 注意用{% ... %}表示指令。
- {{ }}表示变量
- 完成注销功能。
- 清除session
- 跳转
python:
from flask import Flask, render_template, request, 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=True, autoincrement=True) username = db.Column(db.String(20), nullable=False) password = db.Column(db.String(20), nullable=False) telephone = db.Column(db.String(11), nullable=False) #db.create_all() # 增加数据 # user=User(username = 'mis1234',password = '111111',telephone = '12345678900') # db.session.add(user) # db.session.commit() # 查询数据 # user=User.query.filter(user.username == 'mis1234').first() # print(user.username,user.password) # 修改数据 # user=User.query.filter(user.username == 'mis1234').first() # user.password = '000000' # db.session.commit() # 删除数据 # user=User.query.filter(user.username == 'mis1234').first() # db.session.delete(user) # db.session.commit() @app.route('/login/', methods=['GET', 'POST']) def login(): if request.method == 'GET': return render_template('login.html') else: usern = request.form.get('username') passw = request.form.get('password') user = User.query.filter(User.username == usern).first() if user: if user.password == passw: session['user'] = usern return redirect(url_for('base')) else: return 'password error.' else: return 'username is not existed.' @app.route('/register/', methods=['GET', 'POST']) def register(): if request.method == 'GET': return render_template('register.html') else: usern = request.form.get('username') telep = request.form.get('telephone') passw = request.form.get('password') user = User.query.filter(User.username == usern).first() if user: return 'username existed.' else: user1 = User(username=usern, password=passw, telephone=telep) db.session.add(user1) # 数据库操作 db.session.commit() return redirect(url_for('login')) @app.route('/question') def question(): return render_template('question.html') @app.route('/index') def index(): return render_template('index.html') @app.route('/') def base(): return render_template('base.html') @app.context_processor def mycontext(): usern = session.get('user') if usern: return {'username':usern} else: return {} @app.route('/logout/') def logout(): session.clear() return redirect(url_for('base')) if __name__ == '__main__': app.run(debug=True)
html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %}父模板{% endblock %}</title> <script src="{{ url_for('static',filename='js/base.js') }}" type="text/javascript"></script> <link href="{{ url_for('static' ,filename='css/base.css') }}" rel="stylesheet" type="text/css"> {% block head %} {% endblock %} </head> <body id="myBody"> <nav class="body"> <div style="background-color: floralwhite"> <form> <img id="imgs" src="../static/images/chihuo.jpg">       <a class="s" href="{{ url_for('index') }}">首页</a>         <a class="ss" href="{{ url_for('question') }}">下载APP</a>     <input id="sousuo" type="text" name="search" placeholder="请输入搜索内容:"> <button id="sou" type="submit">查找</button> {% if username %} <a class="login" href="#">{{ username }}</a> <a class="register" href="{{ url_for('logout') }}">注销</a> {% else %} <a class="login" href="{{ url_for('login') }}">登录</a> <a class="register" href="{{ url_for('register') }}">注册</a> {% endif %} <img id="myOn_Off" onclick="mySwitch()" src="../static/images/taiyang.png" width="50" height="50"> </form> </div> </nav> {% block main %} {% endblock %} <footer> <div class="footer_box"> Copyright@2017-2027 个人版权,版权所有 作者:JZX telephone:0000-1234567 mobile phone:11111111111 </div> </footer> </body> </html>

注销后:

浙公网安备 33010602011771号