0

我正在尝试对页面进行分页,以便每页显示五个状态。输入这些代码后,分页失败。以下是我的 django 应用程序中用于分页和更新状态的代码。

视图(views.py 文件):

def qask(request):
    extra_data_context={}
    #if there's nothing in the field do nothing.
    if request. method=="POST":
        form =AskForm(request.POST)
        if form.is_valid():
            data=form.cleaned_data
            newask=Ask(user= request.user,status=data['status'],pub_date=datetime.datetime.now())
            newask.save()
        extra_data_context.update({'AskForm':form})
    else:
        form = AskForm()
        extra_data_context.update({'AskForm':form})
    extra_data_context.update({'Asks':Ask.objects.filter(user=request.user)})

    plan=Ask.objects.all()
    paginator=Paginator(plan, 5)

    try:
        page=int(request.GET.get('page','1'))
    except ValueError:
        page=1

    try:
        fp=paginator.page(page)
    except (EmptyPage, InvalidPage):
        fp=paginator.page(paginator.num_pages)
    return render_to_response  ('quik_ask.html',extra_data_context,context_instance=RequestContext(request))
    

模板(html文件):

{% block content %}

{% for Ask in Asks %}
<tr>
    <p>  {{Ask.user}} </p> </strong>
    <p>{{Ask.status}}</p>
    <p> {{Ask.pub_date|timesince }} ago </p> 

</tr>
{% endfor %}

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

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

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

{% endblock %}
4

1 回答 1

0

您的页面数据在fp变量中,但它从未提交到您的模板。相反,您试图从中获取页面数据Asks,但它与代码中的分页器无关。我不确定您要分页什么,但是如果您想对过滤后的数据进行分页,应该是这样的:

plan = Ask.objects.filter(user=request.user)
paginator=Paginator(plan, 5)
###...get you page number
try:
    asks = paginator.page(page)
except (EmptyPage, InvalidPage):
    asks = paginator.page(paginator.num_pages)
extra_data_context.update({'Asks': asks})
于 2012-05-06T23:24:47.650 回答