码迷,mamicode.com
首页 > 其他好文 > 详细

登录之后更新导航

时间:2017-11-24 22:48:41      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:sso   ase   clear   更新   rect   href   插入   lan   style   

  1. 用上下文处理器app_context_processor定义函数
    1. 获取session中保存的值
    2. 返回字典
  2. 在父模板中更新导航,插入
  3. 登录状态判断代码。
  4. 注意用{% ... %}表示指令。
  5. {{ }}表示变量完成注销功能。
    @app.context_processor
    def mycontext():
        usern=session.get(‘user‘)
        if usern:
            return {‘usern‘:usern}
        else:
            return {}
    
        @app.route(‘/logout/‘)
        def logout():
            session.clear()
            return redirect(url_for(‘base‘))
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <title>主页 </title>
        {% if usern %}
                    <li><a href="#">{{ usern }}</a></li>
                    <li><a href="{{ url_for(‘logout‘) }}">注销</a></li>
                {% else %}
                    <li><a href="{{ url_for(‘login‘) }}">登录</a></li>
                    <li><a href="{{ url_for(‘register‘) }}">注册</a></li>
                {% endif %}
    
        </head>
    <body>
    
    </body>
    </html>

     

登录之后更新导航

标签:sso   ase   clear   更新   rect   href   插入   lan   style   

原文地址:http://www.cnblogs.com/caoxinwen092/p/7889840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!