登录之后更新导航


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

父模板 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{% block title %}base{% endblock %}</title>
    <link rel="stylesheet" type="base/css" href="../static/css/base.css">
    <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">

    <script src="../static/js/base.js"></script>
{% block head %}{% endblock %}
</head>
<body id="mybody">

<div class="collapse navbar-collapse" id="ba-example-navbar-collapse-1">
    <ul class="nav navbar-nav">
        <li class="active"><a href="{{ url_for('index') }}">首页<span class="sr-only">(current)</span> </a> </li>

    </ul>
    <form class="navbar-form navbar-left">
        <div class="form-group">
            <input type="text" class="form-control" placeholder="请输入关键字">
        </div>
    <button type="submit" class="btn btn-default"><span class="glyphicon-search" aria-hidden="true" ></span>查找</button>
    </form>
    <ul class="nav navbar-nav navbar-right">
        {% if username %}
            <li><a href="#">{{ username }}</a> </li>
            <li><a href="#">注销</a> </li>
        {% else %}
            <li><a href="{{ url_for('login') }}">登录</a> </li>
            <li><a href="{{ url_for('register') }}">注册</a> </li>
        {% endif %}
    </ul>
</div>

{% block main %}
    <br class="br">
{% endblock %}



<nav class="navbar navbar-inverse navbar-fixed-bottom" role="navigation">
    <ul class="nav navbar-nav">

        <li style="color: bisque;">版权所有@yan</li>
    </ul>
</nav>
</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):
    __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)


# db.create_all()

# @app.route('/')

# def hello_world():
# return 'Hello World!'

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

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


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

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


@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:
        return 'username existed'
    else:
        user = User(username=username, password=password)
        db.session.add(user)
        db.session.commit()
        return redirect(url_for('index'))


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

 

posted on 2017-11-24 15:14  081肖妍  阅读(171)  评论(0)    收藏  举报

导航