[英]python django_tables2 filtering and pagination issue
我正在尝试渲染包含过滤数据表的网页。 过滤应基于用户提交的日期。 对于初始的GET请求,我尝试将initial_date设置为'01 01 2015',并且在提交POST请求时,从POST查询集中提取日期。
我遇到的问题似乎与django_tables2分页有关。 用户在POST请求中提交日期表单后,初始表数据已成功呈现,但是,当我单击表底部的“下一步”按钮时,我收到“异常值:该页面不包含任何结果”。
单击“下一步”按钮出现,以作为GET请求调用GetCustomData()函数,然后使用“ initial_time”而不是过滤的数据。
如何格式化函数,以便后续的GET请求返回初始POST请求中的过滤数据?
views.py
from django.shortcuts import render
from .models import TestResult
import datetime
import django_tables2 as tables
from .forms import DateForm
class SimpleTable(tables.Table):
class Meta:
model = TestResult
attrs = {"class": "paleblue"}
def GetCustomData(request):
form = DateForm()
if request.method == 'POST':
request_time = datetime.datetime.strptime((request.POST['custom_date_day']+' '+request.POST['custom_date_month']+' '+request.POST['custom_date_year']), '%d %m %Y')
time_min = datetime.datetime.combine(request_time, datetime.time.min)
time_max = datetime.datetime.combine(request_time, datetime.time.max)
custom_query = TestResult.objects.filter(timestamp__range=(time_min, time_max))
table = SimpleTable(custom_query)
table.paginate(page=request.GET.get('page', 1), per_page=30)
return render(request, 'custom_results.html', {'table_results': table,
'form': form})
elif request.method == 'GET':
initial_time = datetime.datetime.strptime('01 01 2015', '%d %m %Y')
time_min = datetime.datetime.combine(initial_time, datetime.time.min)
time_max = datetime.datetime.combine(initial_time, datetime.time.max)
custom_query = TestResult.objects.filter(timestamp__range=(time_min, time_max))
table = SimpleTable(custom_query)
table.paginate(page=request.GET.get('page', 1), per_page=30)
return render(request, 'custom_results.html', {'table_results': table,
'form': form})
可能不是最干净的方法,但是我设法通过引用和更新GetCustomData()函数外部的列表来使其工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.