Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

Django自带分页功能的跳页请求时页面接收非常慢

1、环境说明

Django版本:1.10.6

2、现象描述

使用Django自带的分页功能,view和html完全按照官方文档的代码( https://docs.djangoproject.co... )来实现,分页功能可以实现,点击“下一页”或者“上一页”均成功。
但是,直接在url里修改 ?page=2000 来进行大幅度跳页的时候,前端页面接收到结果会非常慢。

3、代码,尽管抄的官网,还是贴一下吧

def restrict_article(request):
    if request.method == 'GET':
        page = request.GET.get('page')
        articles = Article.objects().order_by('-create_time').exclude('content')
        paginator = Paginator(articles, 25) # Show 25 contacts per page
        try:
            contacts = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            contacts = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            contacts = paginator.page(paginator.num_pages)

        return render(request, 'hero/restrict_article.html', {'contacts': contacts})
{% for contact in contacts %}
{# Each "contact" is a Contact model object. #}
{{ contact.title }}<br />
...
{% endfor %}

<div class="pagination">
    <span class="step-links">
        {% if contacts.has_previous %}
            <a href="?page={{ contacts.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
        </span>

        {% if contacts.has_next %}
            <a href="?page={{ contacts.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

不知道各位有没有遇到过这种情况,谢谢!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

数据库用的MongoDB,是MongoDB的索引没加好。刚才换成Mysql没有这个问题。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...