返回顶部
扩大
缩小

Zhang_derek

BBS论坛(三)

3.1.cms用户名渲染和注销功能实现

显示登录的用户名

(1)app/cms/hooks.py

from .views import bp
import config
from flask import session,g
from .models import CMSUser

@bp.before_request
def before_request():
    if config.CMS_USER_ID in session:
        user_id = session.get(config.CMS_USER_ID)
        user = CMSUser.query.get(user_id)
        if user:
            g.cms_user = user

(2)app/cms/init.py

要导入一下才可以,否则不会执行hooks.py里面的代码

import apps.cms.hooks

(3)cms/cms_index.html

<li><a href="#">{{ g.cms_user.username }}<span>[超级管理员]</span></a></li>

注销功能

cms/views.py

@bp.route('/logout/')
@login_required
def logout():
    del session[config.CMS_USER_ID]
    return redirect(url_for('cms.login'))

cms/cms_index.html

 <li><a href="{{ url_for('cms.logout') }}">注销</a></li>

 

3.2.cms模板抽离和个人信息页面完成

(1)cms/cms_base.html

{% from "common/_macros.html" import static %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}{% endblock %}</title>
    <script src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
    <link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="{{ static('cms/css/base.css') }}">
    <script src="{{ static('cms/js/base.js')}}"></script>
    {% block head %}{% endblock %}
</head>
<body>
     <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Zhang_derek论坛管理后台</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">{{ g.cms_user.username }}<span>[超级管理员]</span></a></li>
            <li><a href="{{ url_for('cms.logout') }}">注销</a></li>
          </ul>
          <form class="navbar-form navbar-right">

            <input type="text" class="form-control" placeholder="查找...">
          </form>
        </div>
      </div>
    </nav>

    <div class="container-fluid">
      <div class="row">
          <div class="col-sm-3 col-md-2 sidebar">
              <ul class="nav-sidebar">
                <li class="unfold"><a href="{{ url_for('cms.index') }}">首页</a></li>
                <li class="profile-li">
                    <a href="#">个人中心<span></span></a>
                    <ul class="subnav">
                        <li><a href="{{ url_for('cms.profile') }}">个人信息</a></li>
                        <li><a href="#">修改密码</a></li>
                        <li><a href="#">修改邮箱</a></li>
                    </ul>
                </li>

                      <li class="nav-group post-manage"><a href="#">帖子管理</a></li>

                      <li class="comments-manage"><a href="#">评论管理</a></li>


                      <li class="board-manage"><a href="#">板块管理</a></li>

                      <li class="nav-group user-manage"><a href="#">用户管理</a></li>

                        <li class="nav-group cmsuser-manage"><a href="#">CMS用户管理</a></li>

                      <li class="cmsrole-manage"><a href="#">CMS组管理</a></li>

            </ul>
          </div>
          <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <h1>{% block page_title %}{% endblock %}</h1>
              <div class="main_content">
                  {% block main_content %}{% endblock %}
              </div>
          </div>
      </div>
    </div>
</body>
</html>

(2)cms/cms_index.html

{% extends 'cms/cms_base.html' %}

{% block title %}
    Zhang_derek论坛管理后台
{% endblock %}

{% block page_title %}
    我的论坛
{% endblock %}

(3)cms/views.py

@bp.route('/profile/')
@login_required
def profile():
    return render_template('cms/cms_profile.html')

(4)cms/cms_profile.html

{% extends 'cms/cms_base.html' %}

{% block title %}
    个人信息
{% endblock %}

{% block page_title %}
    {{ self.title() }}
{% endblock %}

{% block main_content %}
    {% set user=g.cms_user %}
    <table class="table table-bordered">
        <tbody>
            <tr>
                <td>用户名:</td>
                <td>{{ user.username }}</td>
            </tr>
            <tr>
                <td>邮箱:</td>
                <td>{{ user.email }}</td>
            </tr>
            <tr>
                <td>角色:</td>
                <td>xxxxxxx</td>
            </tr>
            <tr>
                <td>权限:</td>
                <td>xxxxxx</td>
            </tr>
            <tr>
                <td>加入时间:</td>
                <td>{{ user.join_time }}</td>
            </tr>
        </tbody>
    </table>

{% endblock %}

效果:

 

posted on 2018-06-05 23:47 zhang_derek 阅读(...) 评论(...) 编辑 收藏

导航