登录之后更新导航

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

index.py

import config
from flask import Flask, render_template, url_for, redirect, request,session
from flask_sqlalchemy import SQLAlchemy

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)
    nickname = db.Column(db.String(20))


db.create_all()

@app.route('/')
def shouye():
    return render_template('text.html')


@app.route('/zhuce',methods=['GET','POST'])
def zhuce():
    if request.method == 'GET':
        return render_template('zhuce.html')
    else:
        username = request.form.get('username')
        password = request.form.get('password')
        nickname = request.form.get('nickname')
        user1 = User.query.filter(User.username == username).first()
    if user1:
        return 'username existed'
    else:
        user1 = User(username=username, password=password, nickname=nickname)
        db.session.add(user1)
        db.session.commit()
        return redirect(url_for('denglu'))



@app.route('/denglu',methods=['GET','POST'])
def denglu():
    if request.method == 'GET':
        return render_template('denglu.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
            session.permanent = True
            return redirect(url_for('shouye'))
        else:
            return 'password error'
    else:
        return 'username is not existed'



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


@app.route('/neirong')
def fankui():
    return render_template('fankui.html')

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



if __name__ == '__main__':
    app.run(debug='True')

denglu.html

<!DOCTYPE html>
<html lang="en">
<head>
    {% extends "text.html" %}
    <meta charset="UTF-8">
    <title>登陆</title>
    <link href="https://cdn.bootcss.com/bootstrap/4.0.0-beta/css/bootstrap.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet">
 {% block js %}  <script src="{{ url_for('static',filename='js/Mylogin.js') }}"></script>{% endblock %}

</head>
<body background="../AAA/WeChat%20Image_20171027161942.png" style="background-repeat:no-repeat;background-size: 100% 100%; background-attachment: fixed;">
{% block denglu %}
    <form id="b" method="post" action="{{ url_for('denglu') }}">
<div class="container-fluid " id="denglu">
    <div class="mt-5 pt-5" >
        <div class="card border-primary mb-3 m-auto" style="max-width: 20rem;">
            <div class="card-header text-center">wgd</div>
            <div class="card-body text-primary">
                <h4 class="card-title">用户登录</h4>
                <div class="input-group mb-2 mr-sm-2 mb-sm-0">
                    <div class="input-group-addon"><i class="fa fa-user"></i></div>
                    <input type="text" class="form-control" placeholder="Username" id="uname" required="true" name="username">
                </div>
                <p  id="errorbox1" class="m-0 p-0 text-danger"> &nbsp</p>
                <div class="input-group mb-2 mr-sm-2 mb-sm-0">
                    <div class="input-group-addon"><i class="fa fa-lock"></i></div>
                    <input type="password" class="form-control" placeholder="Password" id="upass"   name="password">
                </div>
                <p id="errorbox2" class="m-0 p-0 text-danger"> &nbsp</p>
                <div class="row">
                    <div class="col-3">
                        <button  class="btn btn-outline-primary" onclick=" MyLogin()" >登录</button>
                    </div>

                    <div class="col-9 text-sm-right">
                        <a href="{{ url_for('zhuce') }}" >注册 </a>|<a href="#" class="text-sm-right"> 忘记密码?</a>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div></form>{% endblock %}
</body>
</html>

父模板text.hrml

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
     <link href="https://cdn.bootcss.com/bootstrap/4.0.0-beta/css/bootstrap.css" rel="stylesheet">
    {% block js %}{% endblock %}
</head>
<body background="../static/img/WeChat%20Image_20171027161942.png" style="background-attachment: fixed;background-size: 100% 100%" >
<nav class="navbar  navbar-expand-lg  bg-light ">
  <a class="navbar-brand" href="#"></a>
  <div class="collapse navbar-collapse  " id="navbarNav">
    <ul class="nav navbar-nav navbar-right">
      <li class="nav-item ">
        <a class="nav-link  " href="{{ url_for("shouye")}}">首页 <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item ">
        <a class="nav-link disabled " href="#">动画</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">音乐</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">游戏</a>
      </li>
    <form class="form-inline"style="line-height: 10%">
        <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
        <button type="button" class="btn btn-secondary" type="submit">搜索</button>
    </form></ul>
  </div>
    <div style="float: right;width: 10%">
        {% if username%}
            <a class="navbar-link disabled"href="#">{{ username }}</a>&nbsp;&nbsp;&nbsp;
            <a class="navbar-link disabled" href="{{ url_for("logout") }}">注销</a>
        {% else %}
                <a class="navbar-link disabled"href="{{ url_for("denglu") }}">登陆</a>&nbsp;&nbsp;&nbsp;
                <a class="navbar-link disabled" href="{{ url_for("zhuce") }}">注册</a>
        {% endif %}

    </div>
</nav>
{% block denglu %}{% endblock %}
{% block zhuce %}{% endblock %}
{% block fankui %}{% endblock %}
<form style="position: fixed;left: 93%;bottom: 10px">
    <div >
        <a class="badge badge-secondary" href="{{ url_for("fankui") }}" style="font-size: 150%">内容发布</a>
    </div>
</form>

</body>
</html>

shouye.html

<!DOCTYPE html>
<html lang="en">
<head>
    {% extends "text.html" %}
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>

        {% block shouye %}{% endblock %}

</body>
</html>

实验截图

 

 

posted on 2017-11-24 11:41  069王国栋  阅读(108)  评论(0编辑  收藏  举报

导航