[英]dynamic pagination with django
Okay so this is first time using pagination with Django and I am trying to prevent it from reloading my view on each page turn. 好的,这是第一次使用Django进行分页,并且我试图防止它在每次翻页时都重新加载我的视图。
I'm handling the pagination in the view like this: 我在这样的视图中处理分页:
page = request.GET.get('page', 1)
print page
paginator = Paginator(list(od.iteritems())[:24], 12)
try:
data = paginator.page(page)
except PageNotAnInteger:
data = paginator.page(1)
except EmptyPage:
data = paginator.page(paginator.num_pages)
print data
save_query_form = SaveQueryForm(request.POST or None)
#if request.method == 'POST':
if save_query_form.is_valid():
profile = save_query_form.save(commit=False)
profile.user = request.user
profile.save()
context = {
"title":"Search",
'data': data,#list(od.iteritems()),
'tools': od_tools.iteritems(),
'methods': od_methods.iteritems(),
'data4': od_data.iteritems(),
'search_phrase': " ".join(instanceValuesString),
'json_dump': js_data,
'form': save_query_form,
}
return render(request, 'results.html', context)
and the pagination is handled in the html: 分页在html中处理:
{% if data.has_other_pages %}
<div id='page-slide'>
<ul class="pagination" start='$offset'>
{% if data.has_previous %}
<li><a href="?page={{ data.previous_page_number }}">«</a></li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in data.paginator.page_range %}
{% if data.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if data.has_next %}
<li><a href="?page={{ data.next_page_number }}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
</div>
{% endif %}
The issue that I am having is that whenever I switch to another page my entire view will run again and the data will does not reflect the original search query and instead defaults to an empty query. 我遇到的问题是,每当我切换到另一页时,我的整个视图将再次运行,并且数据将不会反映原始的搜索查询,而是默认为空查询。
I was wondering if there is a simple way to either handle pagination dynamically or prevent the page reload when toggling between pages? 我想知道是否有一种简单的方法可以动态处理分页或在页面之间切换时防止页面重新加载?
Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。
Update Search Form: 更新搜索表单:
<form action="{% url 'results-view' %}" method="POST" class="autocomplete-me ui-widget" id="myform" >
{% csrf_token %}
<div class="ui-widget" style="text-align:center;">
<input type="text" id="id_q" name="q" placeholder="{{ search_phrase }}">
<br></br>
<div style="text-align:center;" id='adjust-button'>
<input type='submit' class='btn btn-secondary btn-lg' id ='search-btn' value='Search'/>
<a class='btn btn-secondary btn-lg' id ='clear-btn' href="{% url 'inital' %}">Clear</a>
</div>
</div>
</form>
You noted in a comment that you get your search value with instanceValuesString = request.POST.get(u"q").encode('utf-8').strip()
. 您在注释中指出,使用
instanceValuesString = request.POST.get(u"q").encode('utf-8').strip()
获得搜索值。 As one commenter correctly pointed out, this means that when you click your "next page" links (making a GET
request), your view doesn't receive the information it needs to return search results. 正如一位评论者正确指出的那样,这意味着当您单击“下一页”链接(发出
GET
请求)时,视图不会收到返回搜索结果所需的信息。
One way to fix this would be to get your instanceValuesString
from a GET
request instead of a POST
request. 解决此问题的一种方法是从
GET
请求而不是POST
请求中获取instanceValuesString
。 For instance, perhaps your list view is at 例如,也许您的列表视图位于
http://example.com/StuffList
You could look for URLs that provide a search
querystring: 您可以查找提供
search
查询字符串的URL:
http://example.com/StuffList?search=goodstuff
And then grab that in your view: 然后在您的视图中抓住它:
instanceValuesString = request.GET.get('search', None)
if instanceValuesString is not None:
#you have detected a search query; filter results, process request, etc.
One side effect here is that the way you currently construct your next/previous page URLs will break. 这里的一个副作用是,您当前构造下一个/上一个页面URL的方式将会中断。 Consider the example search URL;
考虑示例搜索URL; your current template would construct a link for page 2 like so:
您当前的模板将为第2页构建一个链接,如下所示:
http://example.com/StuffList?search=goodstuff?page=2
This won't work; 这行不通; it should be
&page=2
. 它应该是
&page=2
。 Fortunately there's an easy fix; 幸运的是,有一个简单的解决方法。 check out the second answer to this question: Altering one query parameter in a url (Django) .
请查看此问题的第二个答案: 更改url中的一个查询参数(Django) 。 Using that
url_replace
instead of constructing those links with the basic url
template tag will solve this part of the issue. 使用该
url_replace
而不是使用基本的url
模板标记构造那些链接将解决此部分问题。
This is very much simplified with below package 下面的包大大简化了
http://django-simple-pagination.readthedocs.io/en/latest/ http://django-simple-pagination.readthedocs.io/en/latest/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.