Python 网站开发(14) -- 管理页面

管理页面

管理页面模板共有三个,分别为日志、评论、用户,与以上页面创建方式类似。如日志的管理页面,在www/templates下创建manage_blogs.html

<!-- 继承父模板 '__base__.html' -->
{% extends '__base__.html' %}
<!--jinja2 title 块内容替换-->
{% block title %}日志{% endblock %}
<!--jinja2 beforehead 块内容替换-->
{% block beforehead %}
<!--script中构建vue,向后端API提交日志管理操作相关数据-->
<script>

function initVM(data) {
    var vm = new Vue({
        el: '#vm',
        data: {
            blogs: data.blogs,
            page: data.page
        },
        methods: {
            previous: function () {
                gotoPage(this.page.page_index - 1);
            },
            next: function () {
                gotoPage(this.page.page_index + 1);
            },
            edit_blog: function (blog) {
                location.assign('/manage/blogs/edit?id=' + blog.id);
            },
            delete_blog: function (blog) {
                if (confirm('确认要删除“' + blog.name + '”?删除后不可恢复!')) {
                    postJSON('/api/blogs/' + blog.id + '/delete', function (err, r) {
                        if (err) {
                            return alert(err.message || err.error || err);
                        }
                        refresh();
                    });
                }
            }
        }
    });
    $('#vm').show();
}

$(function() {
    getJSON('/api/blogs', {
        page: {{ page_index }}
    }, function (err, results) {
        if (err) {
            return fatal(err);
        }
        $('#loading').hide();
        initVM(results);
    });
});

</script>

{% endblock %}

<!--jinja2 content 块内容替换-->
{% block content %}
    <div class="uk-grid">
    <div class="uk-width-1-1 uk-margin-bottom">
        <ul class="uk-breadcrumb">
            <li><a href="/manage/comments">评论</a></li>
            <li class="uk-active"><span>日志</span></li>
            <li><a href="/manage/users">用户</a></li>
        </ul>
    </div>

    <div id="error" class="uk-width-1-1">
    </div>

    <div id="loading" class="uk-width-1-1 uk-text-center">
        <span><i class="uk-icon-spinner uk-icon-medium uk-icon-spin"></i> 正在加载...</span>
    </div>

    <div id="vm" class="uk-width-1-1">
        <a href="/manage/blogs/create" class="uk-button uk-button-primary"><i class="uk-icon-plus"></i> 新日志</a>

        <table class="uk-table uk-table-divider">
            <thead>
                <tr>
                    <th class="uk-table-expand uk-text-left"> 标题</th>
                    <th class="uk-text-left">作者</th>
                    <th class="uk-text-left">标签</th>
                    <th class="uk-text-left">创建时间</th>
                    <th class="uk-text-left">操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-repeat="blog: blogs" >
                    <td>
                        <a target="_blank" v-attr="href: '/blog/'+blog.id" v-text="blog.name"></a>
                    </td>
                    <td>
                        <a target="_blank" v-attr="href: '/user/'+blog.user_id" v-text="blog.user_name"></a>
                    </td>
                    <td>
                        <a target="_blank" v-attr="href: '/blogs/'+blog.tag" v-text="blog.tag"></a>
                    </td>
                    <td>
                        <span v-text="blog.created_at.toDateTime()"></span>
                    </td>
                    <td>
                        <a href="#0" v-on="click: edit_blog(blog)">编辑</a>
                        <a href="#0" v-on="click: delete_blog(blog)">删除</a>
                    </td>
                </tr>
            </tbody>
        </table>
        <div class="uk-width-1-1 uk-text-center">
        <ul class="uk-pagination">
            <li v-if="! page.has_previous" class="uk-disabled"><span><i uk-icon="chevron-left"></i></span></li>
            <li v-if="page.has_previous"><a v-on="click: previous()" href="#0"><i uk-icon="chevron-left"></i></a></li>
            <li class="uk-active"><span v-text="page.page_index"></span></li>
            <li v-if="! page.has_next" class="uk-disabled"><span><i uk-icon="chevron-right"></i></span></li>
            <li v-if="page.has_next"><a v-on="click: next()" href="#0"><i uk-icon="chevron-right"></i></a></li>
        </ul>
        </div>
    </div>
    </div>

{% endblock %}

评论的管理页面,在www/templates下创建manage_comments.html

<!-- 继承父模板 '__base__.html' -->
{% extends '__base__.html' %}
<!--jinja2 title 块内容替换-->
{% block title %}评论{% endblock %}
<!--jinja2 beforehead 块内容替换-->
{% block beforehead %}
<!--script中构建vue,向后端API提交评论管理操作相关数据-->
<script>

function initVM(data) {
    $('#vm').show();
    var vm = new Vue({
        el: '#vm',
        data: {
            comments: data.comments,
            page: data.page
        },
        methods: {
            previous: function () {
                gotoPage(this.page.page_index - 1);
            },
            next: function () {
                gotoPage(this.page.page_index + 1);
            },
            delete_comment: function (comment) {
                var content = comment.content.length > 20 ? comment.content.substring(0, 20) + '...' : comment.content;
                if (confirm('确认要删除评论“' + comment.content + '”?删除后不可恢复!')) {
                    postJSON('/api/comments/' + comment.id + '/delete', function (err, r) {
                        if (err) {
                            return error(err);
                        }
                        refresh();
                    });
                }
            }
        }
    });
}

$(function() {
    getJSON('/api/comments', {
        page: {{ page_index }}
    }, function (err, results) {
        if (err) {
            return fatal(err);
        }
        $('#loading').hide();
        initVM(results);
    });
});

</script>

{% endblock %}

<!--jinja2 content 块内容替换-->
{% block content %}

    <div class="uk-width-1-1 uk-margin-bottom">
        <ul class="uk-breadcrumb">
            <li class="uk-active"><span>评论</span></li>
            <li><a href="/manage/blogs">日志</a></li>
            <li><a href="/manage/users">用户</a></li>
        </ul>
    </div>

    <div id="error" class="uk-width-1-1">
    </div>

    <div id="loading" class="uk-width-1-1 uk-text-center">
        <span><i class="uk-icon-spinner uk-icon-medium uk-icon-spin"></i> 正在加载...</span>
    </div>

    <div id="vm" class="uk-width-1-1">
        <table class="uk-table uk-table-justify uk-table-divider">
            <thead>
                <tr>
                    <th class="uk-text-left uk-width-small">作者</th>
                    <th class="uk-text-left">内容</th>
                    <th class="uk-text-left uk-table-expand">创建时间</th>
                    <th class="uk-text-left uk-width-small">操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-repeat="comment: comments" >
                    <td>
                        <span v-text="comment.user_name"></span>
                    </td>
                    <td>
                        <span v-text="comment.content"></span>
                    </td>
                    <td>
                        <span v-text="comment.created_at.toDateTime()"></span>
                    </td>
                    <td>
                        <a href="#0" v-on="click: delete_comment(comment)">删除</a>
                    </td>
                </tr>
            </tbody>
        </table>
        <div class="uk-width-1-1 uk-text-center">
        <ul class="uk-pagination">
            <li v-if="! page.has_previous" class="uk-disabled"><span><i uk-icon="chevron-left"></i></span></li>
            <li v-if="page.has_previous"><a v-on="click: previous()" href="#0"><i uk-icon="chevron-left"></i></a></li>
            <li class="uk-active"><span v-text="page.page_index"></span></li>
            <li v-if="! page.has_next" class="uk-disabled"><span><i uk-icon="chevron-right"></i></span></li>
            <li v-if="page.has_next"><a v-on="click: next()" href="#0"><i uk-icon="chevron-right"></i></a></li>
        </ul>
        </div>
    </div>
{% endblock %}

用户的管理页面,在www/templates下创建manage_users.html

<!-- 继承父模板 '__base__.html' -->
{% extends '__base__.html' %}
<!--jinja2 title 块内容替换-->
{% block title %}用户{% endblock %}
<!--jinja2 beforehead 块内容替换-->
{% block beforehead %}
<!--script中构建vue,向后端API提交用户管理操作相关数据-->
<script>

function initVM(data) {
    $('#vm').show();
    var vm = new Vue({
        el: '#vm',
        data: {
            users: data.users,
            page: data.page
        },
        methods: {
            previous: function () {
                gotoPage(this.page.page_index - 1);
            },
            next: function () {
                gotoPage(this.page.page_index + 1);
            },
            delete_user: function (user) {
                if (confirm('确认要删除用户“' + user.name + '”?删除后不可恢复!')) {
                    postJSON('/api/users/' + user.id + '/delete', function (err, r) {
                        if (err) {
                            return error(err);
                        }
                        refresh();
                    });
                }
            }
        }
    });
}

$(function() {
    getJSON('/api/users', {
        page: {{ page_index }}
    }, function (err, results) {
        if (err) {
            return fatal(err);
        }
        $('#loading').hide();
        initVM(results);
    });
});

</script>

{% endblock %}

<!--jinja2 content 块内容替换-->
{% block content %}
    <div class="uk-grid">
    <div class="uk-width-1-1 uk-margin-bottom">
        <ul class="uk-breadcrumb">
            <li><a href="/manage/comments">评论</a></li>
            <li><a href="/manage/blogs">日志</a></li>
            <li class="uk-active"><span>用户</span></li>
        </ul>
    </div>

    <div id="error" class="uk-width-1-1">
    </div>

    <div id="loading" class="uk-width-1-1 uk-text-center">
        <span><i class="uk-icon-spinner uk-icon-medium uk-icon-spin"></i> 正在加载...</span>
    </div>

    <div id="vm" class="uk-width-1-1">
        <table class="uk-table uk-table-divider">
            <thead>
                <tr>
                    <th class="uk-table-expand uk-text-left">名字</th>
                    <th class="uk-text-left">电子邮件</th>
                    <th class="uk-text-left">注册时间</th>
                    <th class="uk-text-left">操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-repeat="user: users">
                    <td>
                        <span v-text="user.name"></span>
                        <span v-if="user.admin" style="color:#d05">管理员</span>
                    </td>
                    <td>
                        <a v-attr="href: 'mailto:'+user.email" v-text="user.email"></a>
                    </td>
                    <td>
                        <span v-text="user.created_at.toDateTime()"></span>
                    </td>
                    <td>
                        <a href="#0" v-on="click: delete_user(user)">删除</a>
                    </td>
                </tr>
            </tbody>
        </table>
        <div class="uk-width-1-1 uk-text-center">
        <ul class="uk-pagination">
            <li v-if="! page.has_previous" class="uk-disabled"><span><i uk-icon="chevron-left"></i></span></li>
            <li v-if="page.has_previous"><a v-on="click: previous()" href="#0"><i uk-icon="chevron-left"></i></a></li>
            <li class="uk-active"><span v-text="page.page_index"></span></li>
            <li v-if="! page.has_next" class="uk-disabled"><span><i uk-icon="chevron-right"></i></span></li>
            <li v-if="page.has_next"><a v-on="click: next()" href="#0"><i uk-icon="chevron-right"></i></a></li>
        </ul>
        </div>
    </div>
    </div>

{% endblock %}

到这里我们本地的网站就建成了,运行app.py,如果顺利就可以在浏览器中查看我们的网站啦!类似如下截图: web4

web5

web6

最后,运行app.py时如果要进入管理页或写新日志,需要用户是管理员身份。可以从MySQL数据库中将现有的一个用户admin改为1,或者用第4章的test.py新增一个有权限的用户(admin=1)。

[上一章:Python 网站开发(13) -- 日志内容详情页面]
[下一章:Python 网站开发(15) -- 部署网站到远程服务器]


Powered by Aodabo Copyright © 2024.

Zhiyuan Yang. All rights reserved. Privacy | Terms