登录之后更新导航
- 用上下文处理器app_context_processor定义函数
- 获取session中保存的值
- 返回字典
- 在父模板中更新导航,插入登录状态判断代码。
- 注意用{% ... %}表示指令。
- {{ }}表示变量
- 完成注销功能。
- 清除session
- 跳转
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)
nickname = db.Column(db.String(20))
db.create_all()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/denglu/',methods=['GET','POST'])
def denglu():
if request.method == 'GET':
return render_template('denglu.html')
else:
usern = request.form.get('Username')
passw = request.form.get('password')
user=User.query.filter(User.Username==usern).first()
if user:
if user.password == passw:
session['user']=usern
return redirect(url_for('index'))
else:
return u'password error.'
else:
return u'username is not existed'
@app.route('/zhuc/',methods=['GET','POST'])
def zhuc():
if request.method=='GET':
return render_template('zhuc.html')
else:
username = request.form.get('Username')
nickname = request.form.get('nickname')
password = request.form.get('password')
user=User.query.filter(User.username==username).first()
if user:
return u'username existed.'
else:
user = User(username=username,password=password,nickname=nickname)
db.session.add(user)
db.session.commit()
return redirect(url_for('denglu'))
@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('/label/')
def label():
return render_template('label.html')
if __name__ == '__main__':
app.run(debug=True)
html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录界面</title> <link rel="stylesheet" href="../static/css/base.css" type="text/css"> <script src="../static/js/base.js"></script> <base target="_blank" /> {% block head %} {% endblock %} </head> <body id="myBody"> <nav class="nav"> <ul> <img id="me" src="../static/images/sea.png"> <a href="{{ url_for('index') }}" style="font-size: 23px ">首页</a> <a href="">下载</a> <a href="">新闻</a> <a href="{{ url_for('label') }}">发布问答</a> <a>☀</a> <div> {% if username %} <a href="#" style="margin-right: -10px;color:deepskyblue">{{ username }}</a> <a href="{{ url_for('logout') }}" >注销</a> {% else %} <a href="{{ url_for('denglu') }}" style="margin-right: -10px">登录</a> <a href="{{ url_for('zhuc') }}" >注册</a> {% endif %} </div> <img id="on_off" onclick="mySwitch()" src="../static/images/bulbon.gif" > <div> {# <button type="submit" ></button>#} <input type="image" src="../static/images/search.gif" onmousemove="this.src='../static/images/search.gif'" /> <input type="text" name="search"PLACEHOLDER="输入要搜索的内容"> </div> </ul> </nav> {% block main %} <br class="br"> {% endblock %} <div id="bottom"> <a href="">联系我们</a> <a href="">加入我们</a> <a href="">合作伙伴</a> <br> <p>Copyright © 2017. Created by <a href="#" target="_blank">ben</a></p> </div> </div> </body> </html>