我在视图中添加了分页。分页工作正常,但是当我过滤我的数据并导航到下一页时,它没有通过过滤后的数据进行分页,而是进入下一页未进行过滤的页面。
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 %}
有人可以帮我找一个解决方案吗?
答案 0 :(得分:1)
请在模板中进行以下更改,下面的代码将允许分页获取过滤后的值,因此它将通过过滤后的值进行分页。对下一个
进行相同的更改</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>
答案 1 :(得分:0)
这里你需要做的是传递请求值的URL,你必须在模板中进行如下更改
</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>
{% endif %}
对href next进行相同的更改