Search code examples
python-2.7django-viewsdjango-filterdjango-pagination

Filtering is not happening with the pagination


I have added pagination in my view. The pagination is working fine but when I am filtering my data and navigate to the next page it is not paginating through the filtered data, it goes to the next page where the filtering is not happening.

views

 class CashFilter(django_filters.FilterSet):

    class Meta:
        model = NSE_Cash_Trades
        fields = {'dates':['lte','gte'],'order_entry_time':['lte','gte'],'trade_entry_time':['lte','gte'],'trade_modified_time':['lte','gte'],'symbol':['exact'],'client_ac':['exact'],'buys_sell_indicator':['exact'],'participant_code':['exact'],'trade_qty':['exact'],'ctcls_id':['exact'],'order_num':['exact'],'trade_no':['exact']}


 def NSE_Cash_Trades_Search(request):
      f =CashFilter(request.GET, queryset=NSE_Cash_Trades.objects.all())
      paginator = Paginator(f.qs,100)
      page = request.GET.get('page')
      try:
         leads = paginator.page(page)
      except PageNotAnInteger:

         leads = paginator.page(1)
      except EmptyPage:

         leads = paginator.page(paginator.num_pages)

      t = loader.get_template('admin/newftp/search2.html')
      ctx=RequestContext(request,{'filter':f,"leads":leads,})
        return HttpResponse(t.render(ctx))`

template

{% extends "admin/newftp/change_list.html" %}
{% block content %}
<h2><a href="/admin/newftp/nse_cash_trades?status=">Back to Nse Cash trade page</a></h2>
<form action="" method="get">
<!--Required for dynamic total of a column based on search filter
{% for k,v in result1.items %}
<input type="text" name="result1" value="{{ v }}"/>
{% endfor %}
input type="button" name="result1" value= "Total Value" 
-->
  {{ filter.form.as_p }}<br>
  <input type="submit" />
</form>
<div class="pagination">
    <table border="1">

  <tr>
  <th>Sr.No.</th>
  <th>Trade date</th>
  <th>Symbol </th>
  <th>Client Code </th>
  <th>Buy/Sell </th>
  <th>Participant Code</th>
  <th>Trade qty </th>
  <th>CTCL ID</th>
  <th>Order Number</th>
  <th>Trade no </th>
  <th>Order Entry Date Time</th>
  <th>Trade Entry Dt Time</th>
  <th>Trade modified time</th>
  <th>Trade Price</th> 
  <th>Trade Status</th>
  <th>Series</th>
  <th>Security Name</th>
  <th>Instrument Type</th>
  <th>Book Type</th>
  <th>Market Type</th>
  <th>User ID</th>
  <th>Branch ID</th>
  <th>Pro/Cli</th>
  <th>Auction Part Type</th>
  <th>Auction NO</th>
  <th>Sett Period</th>
  <th>Counter Party Id</th>
  </tr>

    {% for line in leads %} 
    <tr>
      <td>{{ forloop.counter}}</td>
      <td>{{ line.dates|date:"d/m/Y"}}</td>
      <td>{{ line.symbol}}</td>
      <td>{{ line.client_ac}} </td>
      <td>{{ line.buys_sell_indicator}}</td>
      <td>{{ line.participant_code}}</td>
      <td>{{ line.trade_qty}}</td>
      <td>{{ line.ctcls_id}}</td>
      <td>{{ line.order_num}}</td>
      <td>{{ line.trade_no}}</td>
      <td>{{ line.order_entry_time|date:"d/m/Y h:m:s"}}</td>
      <td>{{ line.trade_entry_time|date:"d/m/Y h:m:s" }}</td>
      <td>{{ line.trade_modified_time|date:"d/m/Y h:m:s" }}</td>
      <td>{{ line.trade_price }}</td>
      <td>{{ line.trades_status }}</td>
      <td>{{ line.series }}</td>
      <td>{{ line.security_name }}</td>
      <td>{{ line.instruments_type }}</td>
      <td>{{ line.books_type }}</td>
      <td>{{ line.markets_type }}</td>
      <td>{{ line.user_id }}</td>
      <td>{{ line.branchs_id }}</td>
      <td>{{ line.pros_cli }}</td>
      <td>{{ line.auctions_part_type}}</td>
      <td>{{ line.auction_num }}</td>
      <td>{{ line.sett_period }}</td>
      <td>{{ line.counter_party_id }}</td>
      </tr>
      {% endfor %}
</table>
  <span class="step-links">
    {% if leads.has_previous %}
    <a href="?page={{ leads.previous_page_number }}">previous</a>
    {% endif %}

    <span class="current">
      Page {{ leads.number }} of {{ leads.paginator.num_pages }}.
    </span>

    {% if leads.has_next %}
    <a href="?page={{ leads.next_page_number }}">next</a>
        {% endif %}
  </span>
</div>
{% endblock %}

Can anyone please help me to find a solution?


Solution

  • please make the changes given below in the template, the code below will allow the pagination to get the filtered value and so it will paginate through the filtered value. Do the same changes for the next

    </table>
      <span class="step-links">
        {% if leads.has_previous %}
        <a href="?page={{ leads.previous_page_number }}&dates__lte={{request.GET.dates__lte}}&dates__gte={{request.GET.dates__gte}}&order_entry_time__lte={{request.GET.order_entry_time__lte}}&order_entry_time__gte={{request.GET.order_entry_time__gte}}&trade_entry_time__te={{request.GET.trade_entry_time__lte}}&trade_entry_time__gte={{request.GET.trade_entry_time__gte}}&trade_modified_time__lte={{request.GET.trade_modified_time__lte}}&trade_modified_time__gte={{request.GET.trade_modified_time__gte}}&symbol={{request.GET.symbol}}&client_ac={{request.GET.client_ac}}&buys_sell_indicator={{request.GET.buys_sell_indicator}}&participant_code={{request.GET.participant_code}}&trade_qty={{request.GET.trade_qty}}&ctcls_id={{request.GET.ctcls_id}}&order_num={{request.GET.order_num}}&trade_no={{request.GET.trade_no}}">previous</a>