• 作业27-登录之后更新导航


    1. 用上下文处理器app_context_processor定义函数
      1. 获取session中保存的值
      2. 返回字典
    #上下文处理器,可以让所有自定义变量在所有模板中可见.
    @app.context_processor
    def mycontext():
        username= session.get('user')
        if username:
            return{'username':username}  #返回结果必须为字典
        else:
            return{}
    1. 在父模板中更新导航,插入登录状态判断代码。、
      1. 注意用{% ... %}表示指令。
      2. {{ }}表示变量
    {% if username %}
       <li><a href="#">{{ usename }}是不是傻哈哈哈哈</a></li>
       <li><a href="{{url_for('logout')}}">注销</a></li>
    {%  else %}
        <li style="float:right"><a class="active" href="{{url_for('regist')}}">注册</a></li>
        <li style="float:right"><a class="active" href="{{url_for('login')}}">登录</a></li>
    {%  endif %}
    1. 完成注销功能。
      1. 清除session
      2. 跳转
    #注销
    @app.route('/logout/')
    def logout():
        session.clear()
        return redirect(url_for('base')) #重定向
  • 相关阅读:
    Test SLURM
    PGM_Foundations
    PGM-Introduction
    Functional Analysis-Metric Space
    Introduction to Machine Learning
    Time Series Analysis
    Solving Puzzles With HHT From a Engineering Perspective
    Docker-3:Data Volume
    Docker-2:network containers
    Docker-1
  • 原文地址:https://www.cnblogs.com/hegui/p/7889052.html
Copyright © 2020-2023  润新知