【发布时间】:2019-11-08 20:41:55
【问题描述】:
我目前在桌子上使用分页器。我想添加一个选择来过滤表格,但我无法管理如何在不丢失选择的情况下浏览页面。
目前(参见下面的代码)当我通过单击上一个或下一个进行导航时,使用的选项会丢失,并且会显示所有数据。
我必须更改“其他”条件以考虑所选选项。我正在考虑使用全局变量来存储在变量中选择的选项,但它可能会产生副作用,因此不推荐......
views.py
@login_required
def index(request):
# data sent (click on 'search' button or option selected)
if request.POST:
# data from select
selection = request.POST.get('selection', False)
# data from search
ide = request.POST.get('ide', False)
# search
if ide == "":
paginator = Paginator(Preinclusion.objects.all(), 5)
preincluded = paginator.page(1)
else:
paginator = Paginator(Preinclusion.objects.filter(pat_num__startswith = ide),5)
preincluded = paginator.page(1)
# select
if selection == 'Randomized':
print('Randomized')
paginator = Paginator([patient for patient in Preinclusion.objects.all() if patient.is_randomized], 5)
preincluded = paginator.page(1)
elif selection == 'Not randomized':
print('Not randomized')
paginator = Paginator([patient for patient in Preinclusion.objects.all() if not patient.is_randomized], 5)
preincluded = paginator.page(1)
elif selection == 'All patients':
print('All patients')
paginator = Paginator(Preinclusion.objects.all(), 5)
preincluded = paginator.page(1)
if not preincluded:
liste_existe = False # non utilisé dans le template
else:
# first index visited
paginator = Paginator(Preinclusion.objects.all(), 5)
page = request.GET.get('page')
try:
preincluded = paginator.page(page)
except PageNotAnInteger:
preincluded = paginator.page(1)
except EmptyPage:
preincluded = paginator.page(paginator.num_pages)
return render(request, 'randomization/index.html', {'preincluded': preincluded})
我想store为上一个/下一个导航选择的选项。
【问题讨论】: