如何将参数传递给ListView?

时间:2017-04-24 11:14:05

标签: django

如何将变量( min_amount max_amount )从done()传递到SearchAdvertResultView(ListView)

某些对象的

done()方法

def done(self)
    ...
    min_amount = 100
    max_amount = 500
    return redirect(reverse('board:search-result'))

urls.py

...
url(r'^results$',
    SearchAdvertResultView.as_view(),
    name='search-result',
    ),
...

views.py

...
class SearchAdvertResultView(ListView):
    template_name = "board/search_results.html"

    def get_queryset(self):
        ...
        return Adverts.objects.filter(amount__range=(min_amount, max_amount))
...

1 个答案:

答案 0 :(得分:0)

def done(self)
    ...
    min_amount = 100
    max_amount = 500
    urlparams = '?min_amount=%s&max_amount=%s' % (min_amount, max_amount)
    return redirect(reverse('board:search-result')+urlparams)

class SearchAdvertResultView(ListView):
    template_name = "board/search_results.html"

    def get_queryset(self):
        ...
        min_amount = self.request.GET.get('min_amount')
        max_amount = self.request.GET.get('max_amount')
        return Adverts.objects.filter(amount__range=(min_amount, max_amount))