用户文章列表
静态页面返回
在 forum/api/account.py
中添加视图函数,返回静态页面
python
@index_bp.get('/account/article_list.html')
def article_list_view():
return render_template(f'account/article_list.html')
新增 templates/account/article_list.html
html
{% extends 'account/index.html' %}
{% block right_container %}
<h3>文章列表页</h3>
<table class="layui-hide" id="my_article_list-table" lay-filter="my_article_list-table"></table>
{% endblock %}
{% block footer_script %}
<script>
layui.use(function () {
let table = layui.table;
// 创建渲染实例
table.render({
elem: '#my_article_list-table',
url: '/api/v1/my_article_list',
page: {
layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'],
//curr: 5, // 设定初始在第 5 页
groups: 1,
first: false,
last: false
},
cols: [[
{field: 'id', width: 80, title: 'ID'},
{field: 'title', width: 370, title: '文章标题'},
{field: 'create_at', width: 120, title: '状态'},
{field: 'create_at', width: 120, title: '发布时间'},
{field: 'experience', width: 150, title: '操作'}
]]
});
});
</script>
{% endblock %}
接口返回数据
pycon
@index_api.get("/my_article_list")
@jwt_required()
def my_article_list():
page = request.args.get("page", type=int, default=1)
limit = request.args.get("limit", type=int, default=10)
paginate = db.paginate(
current_user.article_list, page=page, per_page=limit, error_out=False
)
return {
"code": 0,
"msg": "获取个人文章成功",
"count": paginate.total,
"data": [r.as_json() for r in paginate.items],
}