简体   繁体   English

python django_tables2过滤和分页问题

[英]python django_tables2 filtering and pagination issue

I am trying to render a web page with a table of filtered data. 我正在尝试渲染包含过滤数据表的网页。 The filtering should be based on a date submitted by the user. 过滤应基于用户提交的日期。 For the initial GET request I have tried setting an initial_date of '01 01 2015' and when a POST request is submitted, the date is extracted form the POST query-set. 对于初始的GET请求,我尝试将initial_date设置为'01 01 2015',并且在提交POST请求时,从POST查询集中提取日期。

The issue I am having appears to be related to django_tables2 pagination. 我遇到的问题似乎与django_tables2分页有关。 The initial table data is successfully rendered after the user submits the date form in the POST request, however when I click the "Next" button on the bottom of the table I receive "Exception Value: That page contains no results". 用户在POST请求中提交日期表单后,初始表数据已成功呈现,但是,当我单击表底部的“下一步”按钮时,我收到“异常值:该页面不包含任何结果”。

Clicking the Next button appears to call the GetCustomData() function as a GET request, which then uses the "initial_time" rather than the filtered data. 单击“下一步”按钮出现,以作为GET请求调用GetCustomData()函数,然后使用“ initial_time”而不是过滤的数据。

How do I format my function so that subsequent GET requests return the filtered data from the initial POST request? 如何格式化函数,以便后续的GET请求返回初始POST请求中的过滤数据?

views.py 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM