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

    1.用上下文处理器app_context_processor定义函数

    1. 获取session中保存的值
    2. 返回字典

    2.在父模板中更新导航,插入登录状态判断代码。

    1. 注意用{% ... %}表示指令。
    2. {{ }}表示变量

    3.完成注销功能。

    1. 清除session
    2. 跳转

    base.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>基础模块</title>
         {% block title %}
         {% endblock %}
         <link rel="stylesheet" href="{{ url_for('static',filename='css/base.css')}}">
        <script src="{{ url_for('static',filename='js/base.js') }}"></script>
         <base target="_blank" />
    {% block head %}
    {% endblock %}
    
    <base href="http://i1.sinaimg.cn/dy/weather/main/index14/007/icons_32_yl/"target="_blank">
    </head>
    <body id="myBody">
    <div class="clearfloat">
    
    
         <nav >
             <img src="w_02_08_00.png" >
            <a href="{{ url_for('index') }}" >首页</a>
             <a href="">下载</a>
             <a href="">图书馆</a>
             <a href="">意见反馈</a>
             <a href="{{ url_for('question') }}">发布问答</a>
             <input type="text" name="search">
             <button type="submit">搜索</button>
             <a href="" >联系我们</a>
         {% if username %}
             <a class="right" href="#">{{ username }}</a>
             <a href="{{ url_for('logout') }}">注销</a>
        {% else %}
             <a class="right" href="{{ url_for('login') }}">登录</a>
             <a href="{{ url_for('zhuce') }}">注册</a>
        {% endif %}
    
    <img id="myOnOff" onclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" width="20px";>
         </nav><hr>
    
    </div>
    
    <br>
    
    <br>
    <br>
    <br><br>
    <h1>
        广州商学院欢迎你!
    </h1>
    <br><br>
    <h1>
        活力广商,筑梦远航!
    </h1>
    <br><br><br>
    
            <div id="footer" style="background-color: lightskyblue;clear: both;text-align: center;"></div>
        <div class="desc" >
            <hr>
            <div class="img" >
                <a href="http://www.gzcc.cn/">
                    <img src="https://timgsa.baidu.com/timg?image&amp;quality=80&amp;size=b9999_10000&amp;sec=1508497686486&amp;di=e2ccca98fa05f9473c84f73db141ad88&amp;imgtype=0&amp;src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F7%2F57ac36088f69c.jpg" width="20px">
                </a>
                <div><a href="http://www.gzcc.cn/">数字广商</a></div>
            </div>
    
            <div class="img">
                <a href="http://www.gzcc.cn/">
                    <img src="https://timgsa.baidu.com/timg?image&amp;quality=80&amp;size=b9999_10000&amp;sec=1508497686476&amp;di=50b7102b085d85e015eff1b2d31436ea&amp;imgtype=0&amp;src=http%3A%2F%2Fimg5.xiazaizhijia.com%2Fwalls%2F20150803%2F1440x900_96ae2cb132cbe37.jpg"></a>
                    <div><a href="http://www.gzcc.cn/">视频校园</a></div>
            </div>
    
            <div class="img" class="clearfloat" class="img-hover">
                <a href="http://www.gzcc.cn/">
                    <img src="https://timgsa.baidu.com/timg?image&amp;quality=80&amp;size=b9999_10000&amp;sec=1508498137537&amp;di=82dbcda3618e2dfd61810d2fb0daf202&amp;imgtype=0&amp;src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F1%2F5754d121da01d.jpg"></a>
                    <div><a href="http://www.gzcc.cn/">画说校园</a></div>
            </div>
    
            <div class="img">
                <a href="http://www.gzcc.cn/">
                    <img src="https://timgsa.baidu.com/timg?image&amp;quality=80&amp;size=b9999_10000&amp;sec=1508498264263&amp;di=55e6cb21f27b29f83d3c9ff9a73b3bae&amp;imgtype=0&amp;src=http%3A%2F%2Fwww.zcool.com.cn%2Fcommunity%2F037b9a359147e96b5b3086ed42c6bee.jpg" width="80px"></a>
                    <div><a href="http://www.gzcc.cn/">全景校园</a></div>
    
    
     </div>
            </div>
    <footer>
     <div class="footer_box">
         Copyright © 2017 广州商学院 All Rights Reserved   版权归本学校所有
     </div>
     </footer>
    {% block main %}
    {% endblock %}
    </body>
    </html>

     py

    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):
        __table__name = '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(50))
    
    #db.create_all()
    
    @app.route('/')
    def index():
        return render_template('index.html')
    
    @app.route('/zhuce/',methods=['GET','POST'])
    def zhuce():
        if request.method =='GET':
            return  render_template('zhuce.html')
        else:
            usern = request.form.get('name')
            teln = request.form.get('password')
            passw = request.form.get('password1')
            user = User.query.filter(User.username==usern).first()
            if user:
                return 'username existed'
            else:
                user= User(username=usern,tel=teln,password=passw)
                db.session.add(user)
                db.session.commit()
                return redirect(url_for('login'))
    
    @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
                    return redirect(url_for('index'))
                else:
                    return u'password error'
            else:
                return u'username is not existed'
    
    @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('index'))
    
    
    @app.route('/question/',methods=['GET','POST'])
    def question():
        return render_template('question.html')
    
    if __name__ == '__main__':
        app.run(debug=True)
  • 相关阅读:
    《C# to IL》第一章 IL入门
    multiple users to one ec2 instance setup
    Route53 health check与 Cloudwatch alarm 没法绑定
    rsync aws ec2 pem
    通过jvm 查看死锁
    wait, notify 使用清晰讲解
    for aws associate exam
    docker 容器不能联网
    本地运行aws lambda credential 配置 (missing credential config error)
    Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?
  • 原文地址:https://www.cnblogs.com/gdlyzx/p/7891756.html
Copyright © 2011-2022 走看看