0

我正在尝试在 Django 1.3 中设置 Django Pagination,它似乎不起作用,因为我返回了超过 5 条记录并且链接被弄乱了

看法:

def directory(request, bought_in_control_panel_id):

    listings = Directory.objects.all().exclude(visible=False).order_by('rank', 'company__name').filter(company__uuid__in=local_uuids)

    paginator = Paginator(listings, 5) # Show 25 contacts per page

    # Make sure page request is an int. If not, deliver first page.
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    # If page request (9999) is out of range, deliver last page of results.
    try:
        page_listings = paginator.page(page)
    except (EmptyPage, InvalidPage):
        page_listings = paginator.page(paginator.num_pages)
    return share.output_page(request, 'directory/directory.html', {'listings': page_listings, 'bought_in_control_panel_id': bought_in_control_panel_id})

模板:

{% block main %}
<link href="/media/css/bootstrap.min.css" rel="stylesheet">
<div class="container-fluid">
<div class="dbx-group">
<div class="dbx-box">
<h3 class="dbx-handle"><div class="tools"></div>  <img src="/media/img/32x32/addressbook.png" style="position:absolute; margin-top:-16px;"><div class="title">Online Suppliers listings {% jms_help_link user 'directory_listings' %}</div></h3>
<h3 class="dbx-handle"><div class="tools"></div>  <a title="Change region" href="{% url change_flag bought_in_control_panel_id iso %}"><img src="/media/img/flags-iso/48/{{ iso|lower }}.png" alt="Change region" align="right" style="margin-top:-45px;"></a><div class="title"></div></h3>
<ul class="dbx-content" width=98%>
    {% if listings %}
        {% for listing in listings %}
            {% if listing.visible %}
            <div class="well well-small" onclick="window.location.href = '{% url view_directory listing.uuid bought_in_control_panel_id %}'" style="cursor:pointer;">
                <div class="row-fluid">
                    <div class="span12">
                        <a style="display:block" href="{% url view_directory listing.uuid bought_in_control_panel_id %}">
                            <h4>{{ listing.company.name }}</h4>
                        </a>
                        <div class="row-fluid">
                            <div class="span10">
                                <blockquote>{{listing.description|truncatewords:30}}</blockquote>
                                <p>Click for more info and downloads</p>
                            </div>
                            <div class="span2">
                                <img src="{% url view_banner listing.uuid %}" alt="">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {% endif %}
        {% endfor %}
        <div class="pagination">
            <span class="step-links">
                {% if listings.has_previous %}
                    <a href="?page={{ listings.previous_page_number }}">previous</a>
                {% endif %}

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

                {% if listings.has_next %}
                    <a href="?page={{ listings.next_page_number }}">next</a>
                {% endif %}
            </span>
        </div>
    {% else %}
        <p align="center"><b>No listings!</b></p>
    {% endif %}
<div style="clear:both;"></div>
</div>
</div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="/media/js/bootstrap.min.js"></script>
{% endblock %}

share.output_page:

def output_page(request, htmlpage, dct={}):
    t = loader.get_template(htmlpage)
    c = RequestContext(request,dct)
    return http.HttpResponse(t.render(c))
4

1 回答 1

0

在 Django 1.3 中,您需要使用:

{% for listing in listings.object_list %}

它在 Django 1.4 中发生了变化。

于 2012-12-18T13:41:53.720 回答