apis.py中添加了Page类用于存储分页信息,用doctest测试成功:
class Page(object):
'''
>>> p1 = Page(100, 1)
>>> p1.page_count
10
>>> p1.offset
0
>> p1.limit
10
>>> p2 = Page(90, 9, 10)
>>> p2.page_count
9
>>> p2.offset
80
>> p2.limit
10
>>> p3 = Page(91, 10, 10)
>>> p3.page_count
10
>>> p3.offset
90
>> p3.limit
10
'''
# 条目总, 跳转页码, 每页容量
def __init__(self, item_count, page_index=1, page_size=10):
self.item_count = item_count
self.page_size = page_size
# 最多能显示的页数
self.page_count = item_count // page_size + (1 if item_count % page_size > 0 else 0)
# 超出页码范围
if (item_count == 0) or (page_index > self.page_count):
self.offset = 0
self.limit = 0
self.page_index = 1
else:
self.page_index = page_index
# 偏移的条目数
self.offset = self.page_size * (page_index - 1)
self.limit = self.page_size
self.has_next = self.page_index < self.page_count
self.has_previous = self.page_index > 1
def __str__(self):
return 'item_count: %s, page_count: %s, page_index: %s, page_size: %s, offset: %s, limit: %s' % (self.item_count, self.page_count, self.page_index, self.page_size, self.offset, self.limit)
__repr__ = __str__
if __name__ == '__main__':
# doctest模块搜索类似于交互式Python会话的文本段然后执行来验证是否如所示的那样
import doctest
doctest.testmod()
在handlers.py中实现获取博客的API和管理页面:
@get('/api/blogs')
async def api_blogs(*, page='1'):
page_index = get_page_index(page)
# select count(id)计算not null值个数
num = await Blog.findNumber('count(id)')
p = Page(num, page_index)
if num == 0:
return dict(page=p, blogs=())
# LIMIT 5,10; 返回第6-15行数据
blogs = await Blog.findAll(orderBy='created_at desc', limit=(p.offset, p.limit))
return dict(page=p, blogs=blogs)
@get('/manage/blogs')
def manage_blogs(*, page='1'):
return {
'__template__': 'manage_blogs.html',
'page_index': get_page_index(page)
}
模板页面首先通过API:GET /api/blogs?page=?拿到Model,然后,通过Vue初始化MVVM,View的容器是#vm,包含一个table,我们用v-repeat可以把Model的数组blogs直接变成多行的<tr>:
{% extends '__base__.html' %}
{% block title %}日志{% endblock %}
{% block beforehead %}
<script>
function initVM(data) {
var vm = new Vue({
el: '#vm',
data: {
blogs: data.blogs,
page: data.page
},
methods: {
edit_blog: function (blog) {
location.assign('/manage/blogs/edit?id=' + blog.id);
},
delete_blog: function (blog) {
// confirm()方法用于显示一个带有指定消息和OK及取消按钮的对话框
if (confirm('确认要删除“' + blog.name + '”?删除后不可恢复!')) {
postJSON('/api/blogs/' + blog.id + '/delete', function (err, r) {
if (err) {
return alert(err.message || err.error || err);
}
// refresh刷新保留之前的缓存内容, 重新加载页面
refresh();
});
}
}
}
});
$('#vm').show();
}
$(function() {
// awesome.js中的getJSON(url,data,callback)把data中的参数转换为形如"a=1&b=2"的键值对
// 然后传递给_httpJSON函数提交
getJSON('/api/blogs', {
page: {{ page_index }}
}, function (err, results) {
if (err) {
return fatal(err);
}
$('#loading').hide();
// results是handlers.py中api_blogs函数返回的dict
initVM(results);
});
});
</script>
{% endblock %}
{% block content %}
<div class="uk-width-1-1 uk-margin-bottom">
<div class="uk-panel uk-panel-box">
<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>
<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-hover">
<thead>
<tr>
<th class="uk-width-5-10">标题 / 摘要</th>
<th class="uk-width-2-10">作者</th>
<th class="uk-width-5-10">创建时间</th>
<th class="uk-width-2-10">操作</th>
</tr>
</thead>
<tbody>
<!-- 可以把v-repeat="blog: blogs"看成循环代码, 所以可以在一个<tr>内部引用循环变量blog -->
<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>
<span v-text="blog.created_at.toDateTime()"></span>
</td>
<td>
<a href="#0" v-on="click: edit_blog(blog)"><i class="uk-icon-edit"></i></a>
<a href="#0" v-on="click: delete_blog(blog)"><i class="uk-icon-trash-o"></i></a> <!-- api_delete_blog待实现 -->
</td>
</tr>
</tbody>
</table>
<div v-component="pagination" v-with="page"></div> <!-- __base__.html里的pagination不是被注释了吗? -->
</div>
{% endblock %}
先为Web App创建一个新博客:
然后浏览器输入“IP地址:端口号/manage/blogs”,就可以显示出来了:
网友评论