1.用上下文处理器app_context_processor定义函数
- 获取session中保存的值
- 返回字典
2.在父模板中更新导航,插入登录状态判断代码。
- 注意用{% ... %}表示指令。
- {{ }}表示变量
3.完成注销功能。
- 清除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=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 home(): return render_template('home.html') @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: if user.password==password: session['user'] = username session.permanent = True return redirect(url_for('home')) else: return 'Password error' else: return 'Username inexistent' @app.route('/logout/') def logout(): session.clear() return redirect(url_for('home')) @app.route('/regist/',methods=['GET','POST']) def regist(): if request.method == 'GET': return render_template("regist.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: user1 = User(username=username, password=password) db.session.add(user1) db.session.commit() return redirect(url_for('login')) @app.route('/question/') def question(): return render_template('question.html') if __name__ == '__main__': app.run(debug=True)
<!DOCTYPE HTML> <html> <head lang="en"> <meta charset="UTF-8"> <title>{% block title %}Base{% endblock %}</title> <link href="{{ url_for('static',filename='CSS/base.css') }}" rel="stylesheet" type="text/css"> <script src="{{ url_for('static',filename='JScript/base.js') }}"></script> {% block head %}{% endblock %} </head> <body id="mybody"> <nav class="meue"> <div style="float: left;"> <img src=" {{ url_for('static',filename='image/smail.png') }}" weight="60" height="60"> </div> <div style="line-height:480%;"> <img id="on_off" src="{{ url_for('static',filename='image/on1.PNG') }}" onclick="MySwitch()" width="30px" > </div> <div > {% if username %} <a href="#"> {{ username }}</a>♡ <a href="{{ url_for('logout') }}"> Logout</a> {% else %} <a href="{{ url_for('login') }}"> Login</a>♡ <a href="{{ url_for('regist') }}"> Regist</a> {% endif %} </div> <div style="line-height:340%;">|</div> <div> <a href="{{ url_for('home') }}">Home</a> <a href="#">Images</a> <a href="#">Videos</a> <a href="#">Article</a> <a href="#">News</a> <a href="{{ url_for('question') }}">Question</a> </div> </nav> <div class="search"> <input type="search" name="search" style="height:20px;width:300px"> <button type="submit" ><img src="{{ url_for('static',filename='image/search.png') }}" width="14" height="14"></button> </div> {% block main %}{% endblock %} <footer class="copyright"> <p>Copyright ☺ All of the company</p> </footer> </body> </html>