문제

I'm trying to solve this problem. There, I'm trying to use django-tables2 pagination with filters. The problem is the filter does not persist with pagination. However, since the pagination links use GET method, I'm trying to cram the "current" value of my_filter in the querystring.

My harebrained idea: when the index view is called, I can unpack the current value of my_filter and re-apply the filter to my table.

I've created a drop down field containing some choices:

MY_CHOICES = (
    ('Apple', 'Apple'),
    ('Ball', 'Ball'),
    ('Cat', 'Cat'),
    )

My model:

class TestModel(models.Model):
    my_choices = models.CharField(max_length=20, choices=MY_CHOICES, default="", verbose_name="Choices")

My form:

class TestFilter(forms.ModelForm):
    class Meta:
        model = TestOrder
        fields = ('my_choices',)

My view:

def index(request):
    if request.method == "POST":
        my_filter = TestFilter(request.POST)
        my_selection = my_filter.cleaned_data['my_choices']
        my_filter = TestFilter(request.POST)
    else:
        my_filter = TestFilter()  

    return render(request, 'my_app/index.html', {'my_filter': my_filter})

Is there is a way to get the "current" value of the my_filter drop down? In other words, is it possible to retrieve data displayed on the web page, after the page has finished loading (i.e. the current selected value of my_filter)?

도움이 되었습니까?

해결책

A related question resolved my query here.

In essence, I should use request.GET for filtering.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top