分区没有在Django中显示

时间:2018-03-21 21:40:40

标签: python django

嗨,我是django的新手,我正在尝试设置分页,但它不起作用,我不知道为什么。这是我的项目代码。 paginator工作它限制为5但只是没有显示页码。当我在地址栏中输入?page = 2时,它的工作原理是我做错了请帮助。

在我的视图类

def post_list(request):
    queryset = Post.objects.all().order_by("-timestamp")
    paginator = Paginator(queryset, 5) # Show 5contacts per page
    page_request_var = "page"
    page = request.GET.get(page_request_var)
    try:
        queryset = paginator.page(page)
    except PageNotAnInteger:
    # If page is not an integer, deliver first page.
        queryset = paginator.page(1)
    except EmptyPage:
    # If page is out of range (e.g. 9999), deliver last page of results.
        queryset = paginator.page(paginator.num_pages)

    context = {
        "object_list": queryset,
        "page_request_var": page_request_var,

}
return render(request, "community.html", context)

在我的模板中

 <div class="container">
    {% for obj in object_list %}
    <div class="row">
        <div class="col-sm-4 col-md-4">
            <div class="post">
                <div class="post-img-content">
                    <img src="http://placehold.it/460x250/e67e22/ffffff&text=HTML5" class="img-responsive" />
                    <span class="post-title"><b><a href='{{ obj.get_absolute_url }}'>{{ obj.title }}</a></b><br />
                    </span>
                </div>
                <div class="content">
                    <div class="author">
                        By <b>changeme</b> |
                        <small> {{ obj.timestamp|timesince }} ago</small>
                    </div>
                    <div>
                        {{ obj.content|linebreaks|truncatechars:120 }}
                    </div>
                    <div>
                        <a href="{{ obj.get_absolute_url }}" class="btn btn-warning btn-sm">Read more</a>
                    </div>
                    <hr />
                </div>
            </div>
        </div>


    </div>
    {% endfor %}
    </div>
    <div class="container"> 

    <div class="pagination">
    <span class="step-links">
        {% if queryset.has_previous %}
            <a href="?{page_request_var}=1">&laquo; first</a>
            <a href="?{page_request_var}={{contacts.previous_page_number}}">previous</a>
        {% endif %}

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

        {% if queryset.has_next %}
            <a href="?{page_request_var}={{queryset.next_page_number}}">next</a>
            <a href="?{page_request_var}={{queryset.paginator.num_pages }}">last &raquo;</a>
        {% endif %}
    </span>
</div>
</div>

它只显示页面。 我究竟做错了什么 ?

1 个答案:

答案 0 :(得分:2)

即使您的上下文中没有queryset,您也会在模板中使用object_list。如您所见,您将context = { "object_list": queryset, "page_request_var": page_request_var, } 设置为object_list

queryset

只需在模板中使用object_list而不是queryset

或将context = { "queryset": queryset, "page_request_var": page_request_var, } 更改为private CompletableFuture<Collection<Profiles>> getProfiles(QueryType query){ return supplyAsync(() -> { try { Collection<Profiles> profiles = this.gateway.profiles(query); } catch (Exception e) { throw new Exception("test", e); } }

public CompletableFuture<Collection<Results>> handleRequest(
            QueryType query) {
        CompletableFuture<Collection<Profiles>> profiles =
                getProfiles(query);
         profiles.thenCompose(
            //Iterate over all profiles and call another function that returns completable future with the argument profiles.ID
         )
}