Django分页器中的vi

2024-09-29 21:49:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有搜索代码和分页代码在同一个视图中,所以每当我试图查看分页的第二页时,就会得到标准的搜索页面。。。 在django教程中,每次调用视图时都会生成列表,在我的例子中,由于搜索表单总是与分页代码一起加载,所以当请求第二次分页时,列表会被删除。。。 我能做什么?在

下面是简化的代码,这样您就可以了解到:

def main_page(request):
anuncios = []


#This is the search 
if request.method == 'POST':
    anuncios = Anuncio.objects.all().order_by('votos').reverse()[0:20]

# Pagination
paginator = Paginator(anuncios, 2)

page = request.GET.get('page')
try:
    p_anuncios = paginator.page(page)
except PageNotAnInteger:
    # If page is not an integer, deliver first page.
    p_anuncios = paginator.page(1)
except EmptyPage:
    # If page is out of range (e.g. 9999), deliver last page of results.
    p_anuncios = paginator.page(paginator.num_pages)

return render_to_response('main_page.html', RequestContext(request,
    {'form':form,
    'anuncios': p_anuncios,
    'vazio':vazio
    })
    )

谢谢


Tags: of代码form视图列表ifismain
1条回答
网友
1楼 · 发布于 2024-09-29 21:49:24

如果有一个表单包含一些影响paginator列表的字段,可以执行以下操作

<input type="hidden" name="current_page" value="{{ anuncios.number }}" />
<input type="submit" name="_next" value="next" />

在python中

^{pr2}$

然后,您可以添加一个隐藏的输入字段name=“page”,其中包含当前页面值和vor导航

相关问题 更多 >

    热门问题