gpt4 book ai didi

python - 分页无法在Django中使用

转载 作者:行者123 更新时间:2023-12-03 08:57:39 25 4
gpt4 key购买 nike

在我的自定义模板中,对其应用了分页。页面上显示的列表比我在其上使用的分页要大得多。显示列表的限制正确地扭曲了,但是当我单击下一个按钮时,它就处于else条件。

views.py:-

@csrf_exempt
def search(request):
if request.method == 'POST':
getchoice = request.POST['userchoice']
getfirstdate = request.POST['firstdate']
getseconddate = request.POST['seconddate']


if getchoice == '0':
getdata = applicationform.objects.filter(date__gte=getfirstdate , date__lte=getseconddate)
##### PAGINATION
searchpagination = Paginator(getdata ,5)
page = request.GET.get('searchpage')
try:
searchcontacts = searchpagination.page(page)
except PageNotAnInteger:
searchcontacts = searchpagination.page(1)
except EmptyPage:
searchcontacts = searchpagination.page(searchpagination.num_pages)

if getdata:
return render_to_response('registration/search_page.html', {'getdata':getdata ,'getchoice':getchoice ,'searchcontacts': searchcontacts})
else:
return HttpResponse('NO ITEMS FOUND ON THIS DATE')
else :
return render_to_response('registration/search_page.html')

自定义模板:-
{% if searchcontacts.has_previous %}
<a href="?searchpage={{ searchcontacts.previous_page_number }}">PREVIOUS</a>
{% endif %}
{% if searchcontacts.has_next %}
<a href="?searchpage={{ searchcontacts.next_page_number }}">NEXT</a>
{% endif %}

最佳答案

单击NEXT将是GET请求,因此将在else部分中进行。

要继续搜索下一页,您需要将搜索参数存储在 session 中或通过url传递。然后在filter()查询中使用它。

关于python - 分页无法在Django中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19151795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com