zoukankan      html  css  js  c++  java
  • 登录之后更新导航

      1. 用上下文处理器app_context_processor定义函数
        1. 获取session中保存的值
        2. 返回字典
      2. 在父模板中更新导航,插入登录状态判断代码。、
        1. 注意用{% ... %}表示指令。
        2. {{ }}表示变量
      3. 完成注销功能。
        1. 清除session
        2. 跳转
          

        处理器:

    @app.context_processor
    def mycontext():
        usern=session.get('user')
        if usern:
            return{'username':usern}
        else:
            return{}

    base :

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <title>主页 {% block logintitle %}
        {% endblock %}
            {% block registtitle %}
            {% endblock %}
            {% block questiontitle %}
            {% endblock %}
        </title>
    
        <link href="{{ url_for('static',filename='css/base.css') }}" rel="stylesheet" type="text/css">
        <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/base.css') }}">
        <script src="{{ url_for('static',filename='js/myswitch.js') }}"></script>
        {% block loginhead %}
        {% endblock %}
        {% block registhead %}
        {% endblock %}
        {% block questionhead %}
        {% endblock %}
    </head>
    
    <body id="myBody" style="background-image: url(../static/images/basep.jpg)">
    
    <ul>
        <li><img class="ico" src="../static/imgages/yellow.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>
        {% if username %}
            <li><a href="#">{{ username }}</a></li>
            <li><a href="{{ url_for('logout') }}">注销</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()" <img id="myOnOff" onclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" width="40px"></li>
    </ul>
    <footer>
        <div class="footer_box">
            <p>Posted by: cls</p>
        </div>
    </footer>
    {% block loginbody %}  {% endblock %}
    {% block registbody %}  {% endblock %}
    {% block questiontbody %} {% endblock %}
    </body>
    </html>

    处理器:

    @app.route('/logout')
    def logout():
        session.clear()
        return redirect(url_for('base'))

  • 相关阅读:
    html5 td中的5它空隙待解决
    转:能说明你的Javascript技术很烂的五个原因
    css字体中文、英文、Unicode名对照表
    相对定位一个例子,仿淘宝商品列表中的简单效果
    利用Javascript判断操作系统的类型
    百度web前端面试题之求两个数的最大公约数和最小公倍数
    程序员在群询问破解软件
    转:IE10初探
    学习js在线html(富文本)编辑器
    json学习笔记
  • 原文地址:https://www.cnblogs.com/1244581939cls/p/7889858.html
Copyright © 2011-2022 走看看