博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
登录之后更新导航
阅读量:4676 次
发布时间:2019-06-09

本文共 3484 字,大约阅读时间需要 11 分钟。

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

index.py

import configfrom flask import Flask, render_template, url_for, redirect, request,sessionfrom flask_sqlalchemy import SQLAlchemyapp = 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 shouye():    return render_template('text.html')@app.route('/zhuce',methods=['GET','POST'])def zhuce():    if request.method == 'GET':        return render_template('zhuce.html')    else:        username = request.form.get('username')        password = request.form.get('password')        nickname = request.form.get('nickname')        user1 = User.query.filter(User.username == username).first()    if user1:        return 'username existed'    else:        user1 = User(username=username, password=password, nickname=nickname)        db.session.add(user1)        db.session.commit()        return redirect(url_for('denglu'))@app.route('/denglu',methods=['GET','POST'])def denglu():    if request.method == 'GET':        return render_template('denglu.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('shouye'))        else:            return 'password error'    else:        return 'username is not existed'@app.route('/logout/')def logout():    session.clear()    return redirect(url_for('shouye'))@app.route('/neirong')def fankui():    return render_template('fankui.html')@app.context_processordef mycontext():    usern=session.get('user')    if usern:        return {
'username':usern} else: return {}if __name__ == '__main__': app.run(debug='True')

denglu.html

    {% extends "text.html" %}    
登陆
{% block js %} {% endblock %}{% block denglu %}
wgd

用户登录

 

 

{% endblock %}

父模板text.hrml

    
Title
{% block js %}{% endblock %}
{% block denglu %}{% endblock %}{% block zhuce %}{% endblock %}{% block fankui %}{% endblock %}

shouye.html

    {% extends "text.html" %}    
首页 {% block shouye %}{% endblock %}

实验截图

 

 

转载于:https://www.cnblogs.com/wgd0069/p/7889861.html

你可能感兴趣的文章
2017年6月2号课堂笔记
查看>>
github
查看>>
poj1015【DP.......无奈了】
查看>>
C#性能优化的一些技巧
查看>>
PAT 甲级 1024 Palindromic Number
查看>>
信息安全经典书籍
查看>>
ios坐标位置转换
查看>>
Java多线程—JUC原子类
查看>>
C#中常用到的时间函数(天数差、星期几等)
查看>>
如何理解一台服务器可以绑定多个ip,一个ip可以绑定多个域名
查看>>
改进delphi中的RoundTo函数
查看>>
Microsoft Visual SourceSafe使用经验
查看>>
威尔逊定理及证明
查看>>
[LeetCode] Peeking Iterator
查看>>
Understanding Unix/Linux Programming-用户程序play_again4.c
查看>>
算法总结
查看>>
WPF中使用USERCONTROL
查看>>
图片,base64 互转
查看>>
ES6 有什么新东西
查看>>
cache—主存—辅存三级调度模拟
查看>>