3

I'm using django-filter to output filtered results of my model. No issues there. Next step is adding a paginator.. though struggling for days now.

views.py:

def funds_overview(request):
 f = FundFilter(request.GET, queryset=Fund.objects.all()).qs
 paginator = Paginator(f, 5)
 page = request.GET.get('page')
 funds = paginator.get_page(page)
 return render(request, 'funds/funds.html', {'filter': funds})

funds.html:

<form method="get">
<div class="well">
 <h4 style="margin-top: 0">Search Criteria</h4>

  <div class="row">
   <div class="form-group col-sm-4 col-md-3">
    {{ filter.form.fund_name.label_tag }}
    {% render_field filter.form.fund_name class="form-control" %}
   </div>
  </div>

<button type="submit" class="btn btn-primary">
  <span class="glyphicon glyphicon-search"></span> Search
</button>

{% for fund in filter.qs %}
<p>{{fund.name}} </p>

{% empty %}

No funds match your search criteria

{% endfor %}

Result in the browser The line "no funds match your search criteria" ..

can anyone help? i assume something is wrong with calling the GET request twice?

Thanks !

TMD
  • 191
  • 1
  • 2
  • 14

1 Answers1

2

You're passing a new context where filter is now a page, so when using {% for fund in filter.qs %} your accessing a field that does not exist in a page object, so there will be a empty loop.

Try to change the filter.qs for just filter.

It will solve the pagination problem, but there is another one emerging with the context passed to the render function. As shown below you should add another context variable to keep your page showing the filtered form.

views.py:

def funds_overview(request):
 funds = FundFilter(request.GET, queryset=Fund.objects.all()).qs
 paginator = Paginator(f, 5)
 page = request.GET.get('page')
 fund_page = paginator.get_page(page)
 return render(request, 'funds/funds.html', {'filter': funds, 'page': fund_page})

funds.html:

<form method="get">
<div class="well">
 <h4 style="margin-top: 0">Search Criteria</h4>

  <div class="row">
   <div class="form-group col-sm-4 col-md-3">
    {{ filter.form.fund_name.label_tag }}
    {% render_field filter.form.fund_name class="form-control" %}
   </div>
  </div>

<button type="submit" class="btn btn-primary">
  <span class="glyphicon glyphicon-search"></span> Search
</button>

{% for fund in page %}
<p>{{fund.name}} </p>

{% empty %}

No funds match your search criteria

{% endfor %}
Page {{ page.number }} of {{ page.paginator.num_pages }}
Bernardo Duarte
  • 4,074
  • 4
  • 19
  • 34
  • 1
    Hi Bernardo. it partly helps: Now I see the first 5 line of the database. Though no "page 1 out of 2" or alike, and (as was before) the search field/forms don't appear – TMD Feb 17 '19 at 15:36
  • @TMD I've updated the answer to show a possible solution to your case. – Bernardo Duarte Feb 17 '19 at 16:07
  • Thanks Bernardo. It now shows indeed 'page x of y' though I can't flip through it..? – TMD Feb 17 '19 at 20:57
  • @TMD That, I believe, would be a whole other question, and I think it is better for you to try it first for yourself ;D Anyway, I guess [this link](https://docs.djangoproject.com/en/2.1/topics/pagination/) should help. If my answer helped you achieve your question goal, please don't forget to upvote and mark as solved. – Bernardo Duarte Feb 17 '19 at 21:10
  • @BernardoDuarte thanks this helped! My issue is I was using the for loop with my filter payload instead of my pagination payload. – kingmilo Jun 30 '21 at 09:44