登录之后更新导航
Posted 018林彬
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了登录之后更新导航相关的知识,希望对你有一定的参考价值。
- 用上下文处理器app_context_processor定义函数
- 获取session中保存的值
- 返回字典
- 在父模板中更新导航,插入登录状态判断代码。、
- 注意用{% ... %}表示指令。
- {{ }}表示变量
- 完成注销功能。
- 清除session
- 跳转
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=TabError, autoincrement=True) username = db.Column(db.String(20), nullable=False) password = db.Column(db.String(20), nullable=False) nickname = db.Column(db.String(20), nullable=True) # db.create_all() #是否链接成功 # 数据添加 # user=User(username=‘ma‘,password=‘gg‘) # db.session.add(user) # db.session.commit() # # # 数据更新 # # user = User.query.filter(User.username==‘hh‘).first() # user.password=‘gg‘ # db.session.commit() # # # 数据查询 # user = User.query.filter(User.username==‘ma‘).first() # print(user.username,user.password) # # 数据删除 # user = User.query.filter(User.username==‘ma‘).first() # db.session.delete(user) # db.session.commit() # session会话连接 # filter()过滤器 # route制定路径和函数之间的关系 # def定义一个变量 @app.route(‘/‘) def switch(): return render_template(‘switch.html‘) @app.route(‘/login/‘, methods=[‘GET‘, ‘POST‘]) # 跳转登陆,methods定义它有两种请求方式 def login(): if request.method == ‘GET‘: return render_template(‘login.html‘) else: username = request.form.get(‘user‘) password = request.form.get(‘psw‘) user = User.query.filter(User.username == username).first() # 判断用户名是否存在 if user: if user.password == password: session[‘user‘]=username session.permanent = True return redirect(url_for(‘switch‘)) else: return ‘用户名不存在‘ @app.context_processor def mycontext(): username=session.get(‘user‘) if username: return {‘username‘:username} else: return {} @app.route(‘/register/‘, methods=[‘GET‘, ‘POST‘]) # 跳转注册,methods定义它有两种请求方式 def register(): if request.method == ‘GET‘: return render_template(‘register.html‘) else: username = request.form.get(‘user‘) password = request.form.get(‘psw‘) nickname = request.form.get(‘nickname‘) user = User.query.filter(User.username == username).first() # 判断用户名是否存在 if user: return u‘该用户已存在‘ else: user = User(username=username, password=password, nickname=nickname) db.session.add(user) db.session.commit() return redirect(url_for(‘login‘)) @app.route(‘/logout/‘) def logout(): session.clear() return render_template(‘switch.html‘) @app.route(‘/wenda/‘) def wenda(): return render_template(‘wenda.html‘) if __name__ == ‘__main__‘: app.run(debug=True)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> 首页 {% block logintitle %}{% endblock %} {% block registertitle %}{% endblock %} {% block wendatitle %}{% endblock %} </title> <script> function mySwitch() { var oBody= document.getElementById("myBody"); var oOnoff=document.getElementById("myOnOff"); if(oOnoff.src.match("bulbon")){ oOnoff.src="http://www.runoob.com/images/pic_bulboff.gif"; oBody.style.background="black"; oBody.style.color="yellow"; }else{oOnoff.src="http://www.runoob.com/images/pic_bulbon.gif"; oBody.style.background="lightcyan"; oBody.style.color="black"; } } </script> <link rel="stylesheet" type="text/css" href="../static/css/switch.css"> {% block loginhead %}{% endblock %} {% block registerhead %}{% endblock %} {% block wendahead %}{% endblock %} </head> <body id="myBody" style="background-color: lightcyan"> <nav> <img src="{{ url_for(‘static‘,filename=‘image/gzsxy.png‘) }}" alt="" width="20px"> <script> document.write(Date()) </script> <br> <p>{{ username }}context</p> <a href="{{ url_for(‘switch‘) }}">首页</a> {% if username %} <a href="#">{{ username }}</a> <a href="{{ url_for(‘logout‘) }}">注销</a> {% else %} <a href="{{ url_for(‘login‘) }}">登录</a> <a href="{{ url_for(‘register‘) }}">注册</a> {% endif %} <a href="{{ url_for(‘wenda‘) }}">问答</a> <input type="text" placeholder="请输入关键字"> <button type="submit">搜索</button> </nav> {% block loginbody %}{% endblock %} {% block registerbody %}{% endblock %} {% block wendabody %}{% endblock %} </body> </html>
以上是关于登录之后更新导航的主要内容,如果未能解决你的问题,请参考以下文章