当前位置: 移动技术网 > IT编程>网页制作>CSS > html页面在mako页面获取到的是一串很长的字符串,而不是需要的页码数如何解决?

html页面在mako页面获取到的是一串很长的字符串,而不是需要的页码数如何解决?

2017年12月27日  | 移动技术网IT编程  | 我要评论

遇到的问题:我看网上html页面大家通过page2.next_page_number和page2.preivous_page_number获取上一页和下一页的页码,而我在mako页面获取到的是一串很长的字符串,而不是需要的页码数,我的解决方案是通过后端传过来上一页和下一页的页码。

1.网上我找到的大多数是针对html页面来写的基于django框架的分页,我对着网上的针对自己的情况进行了修改。

2.首先看一下后端代码,别看这么多,其实起作用的就4句话,我都打了注解。

def modelmanager(request):
    apps = appmanager.get_apps_dict(request.user)
    models = model.objects.all() #查询所有数据
    paginator = Paginator(models, 10)  # 每页显示10条纪录
    page = request.GET.get('page')  # 获取客户端请求传来的页码
    next=''
    pre=''
    try:
        customer_list = paginator.page(page)  # 返回用户请求的页码对象
        if customer_list.has_next():
            next=customer_list.next_page_number()
        else:
            next=customer_list.number
        if customer_list.has_previous():
            pre=customer_list.previous_page_number()
        else:
            pre='1'
    except PageNotAnInteger:
        customer_list = paginator.page(1)  # 如果请求中的page不是数字,也就是为空的情况下
    except EmptyPage:
        customer_list = paginator.page(paginator.num_pages)  # 如果请求的页码数超出paginator.page_range(),则返回paginator页码对象的最后一页
    return render('modelmanager.mako', request, {
        'models': customer_list,
        'date': datetime.datetime.now(),
        'apps': apps,
        'is_embeddable': request.GET.get('is_embeddable', False),
        'pre':pre,
        'next':next,
    })

3.分页的写法,这是在mako模板中的写法。

<p class="pagination">
    <nav>
        <ul class="pagination">
            % if models.has_previous:
                <li ><a href="?page=${pre}" aria-label="Previous"><span
                        aria-hidden="true">&laquo;</span></a></li>
            % endif
            % for page_num in models.paginator.page_range:
                % if page_num == models.number:
                    <li class="active"><a href="?page=${page_num}">${page_num}<span class="sr-only">(current)</span></a>
                    </li>
                % else:
                    <li ><a href="?page=${page_num}">${page_num}<span class="sr-only">(current)</span></a></li>
                % endif
            % endfor
            % if models.has_next:
                <li ><a href="?page=${next}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a>
                </li>
            % endif
        </ul>
    </nav>
</p>

4.数据的展示,这个通过foreach将数据展示出来就可以。

<p style="width:100%">
    <table id="models_manager" class="table table-striped table-bordered table-condensed" style="width:100%;table-layout:fixed;">
        <thead>
        <tr>
            <th class="width1">序号</th>
            <th class="width2">名称</th>
            <th class="width3">类型</th>
            <th class="width1">所有者</th>
            <th class="width2">日期</th>
            <th class="width3">训练集</th>
            <th class="width3">模型路径</th>
            <th class="width3">训练参数</th>
            <th class="width3">评估表名</th>
            <th class="width3">操作</th>
        </tr>
        </thead>
        <tbody>
            % for model in models:
                <tr>
                    <td id="${model.id}" name="id" >${model.id}</td>
                    <td id="${model.m_name}" name="m_name" data-toggle="tooltip" data-placement="bottom" title="${model.m_name}">${model.m_name}</td>
                    <td id="${model.m_type}" name="m_type" data-toggle="tooltip" data-placement="bottom" title="${model.m_type}">${model.m_type}</td>
                    <td id="${model.m_user}" name="m_user" data-toggle="tooltip" data-placement="bottom" title="${model.m_user}">${model.m_user}</td>
                    <td id="${model.m_time}" name="m_time" data-toggle="tooltip" data-placement="bottom" title="${model.m_time}">${model.m_time}</td>
                    <td id="${model.m_traindata}" names="m_traindata" data-toggle="tooltip" data-placement="bottom" title="${model.m_traindata}">${model.m_traindata}</td>
                    <td id="${model.m_path}" name="m_path" data-toggle="tooltip" data-placement="bottom" title="${model.m_path}">${model.m_path}</td>
                    <td id="${model.m_arguments}" name="m_arguments" data-toggle="tooltip" data-placement="bottom" title="${model.m_arguments}" >${model.m_arguments}</td>
                    <td id="${model.evaluate_tablename}" name="evaluate_tablename" data-toggle="tooltip" data-placement="bottom" title="${model.evaluate_tablename}" >${model.evaluate_tablename}</td>
                    <td >
                        <button style="width:85px;height:35px" type="button" class="btn btn-primary" id='${model.evaluate_tablename}'
                                onclick="show_evaluate(event,${model.evaluate_tablename})">评估结果
                        </button>
                    </td>
                </tr>
            % endfor
        </tbody>
    </table>
</p>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网