- 用上下文处理器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=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 daohang(): return render_template('daohang.html') @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') # nickname=request.form.get('nickname') user = User.query.filter(User.username == username).first() if user: session['user']=username session.permanent=True return redirect(url_for('daohang')) else: return 'error username or password' # if user: # return 'username existed' # else: # user=User(username=username,password=password) # db.session.add(user) # db.session.commit() return redirect(url_for('daohang')) @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('regist')) @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') # nickname=request.form.get('nickname') 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.route('/biaoqian/') def biaoqian(): return render_template('biaoqian.html') if __name__ == '__main__': app.run(debug=True)
首页.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %}{% endblock %}首页</title> <link type="text/css" rel="stylesheet" href="{{ url_for('static',filename='/css/index.css') }}"> <script src="{{ url_for('static',filename='js/moonlight.js') }}"></script> {% block head %}{% endblock %} </head> <body id="myBody"> <nav> <img src="http://cdn2.jianshu.io/assets/web/logo-58fd04f6f0de908401aa561cda6a0688.png" width="40px"> <a href="{{ url_for('daohang') }}">首页</a> <a href="http://www.jianshu.com/">下载app</a> <input type="text" name="search"> <button type="submit">搜索</button> {% if username %} <a href="#">{{ username }}</a> <a href="{{ url_for('logout') }}">注销</a> {% else %} <a href="{{ url_for('login') }}">登录</a> <a href="{{ url_for('regist') }}">注册</a> {% endif %} <a href="{{ url_for('biaoqian') }}">发布问答</a> <img id="myOnOff" src="http://www.runoob.com/images/pic_bulbon.gif" onclick="mySwitch()" width="20px"> </nav> {% block main %}{% endblock %} <p>{{ username }}contextx</p> </body> </html>