Aule
Aule

Reputation: 31

Remember URL parameters in django-tables2 with django-filter

This is my view:

class FilteredReclamationListView(FilterView, SingleTableView):
    table_class = ReclamationTable
    model = ReclamationMainModel
    template_name = 'reclamation_table/index.html'
    filterset_class = ReclamationFilter
    table_pagination = {
    'per_page': 50
    }
    def get_table_data(self):
        return self.object_list
    def get_queryset(self):
        return self.model.objects.filter(archive=False).order_by('-id')

Is it possible to remember the URL parameter in this case? How?

I would like to have a situation that the user goes to another view and when he is back he will see his last query/filter. I read about sessions and request.GET.urlencode(), but I can't apply that in my view.

Upvotes: 1

Views: 856

Answers (2)

Aule
Aule

Reputation: 31

I found a soluton to my problem. My first view looks like:

class FilteredReclamationListView(FilterView, SingleTableView):
    table_class = ReclamationTable
    model = ReclamationMainModel
    template_name = 'reclamation_table/index.html'
    filterset_class = ReclamationFilter
    table_pagination = {
        'per_page': 50
    }

    def get_table_data(self):
        return self.object_list

    def get_queryset(self):
        self.request.session['urltoremember'] = self.request.get_full_path()
        return self.model.objects.filter(archive=False).order_by('-id')

And in the second view I make this:

class ReclamationDetailView(DetailView):
    model = ReclamationMainModel
    context_object_name = 'reclamation_data'

    def get_context_data(self, **kwargs):
        context = super(ReclamationDetailView, self).get_context_data(**kwargs)
        context['filter'] = self.request.session['urltoremember']
        return context

In the template I change the link to go back to FilteredReclamationListView to {{ filter }}:

a href="{{ filter }}" class="btn btn-default navbar-btn" id="menu-toggle">back</a>

Upvotes: 2

Walucas
Walucas

Reputation: 2578

Aule, you could store the filter on the session.

request.session['filter'] = myFilter

Upvotes: 1

Related Questions