Django分页过滤的搜索结果 [英] Django pagination in filtered search post results
问题描述
我有一个看法,过滤掉发布搜索表单的结果:
I have a view that filters out results for a posted search form:
def profile_advanced_search(request):
args = {}
if request.method == "POST":
form = AdvancedSearchForm(request.POST)
qs=[]
if form.is_valid():
cd = form.cleaned_data
s_country=cd['country']
s_province=cd['province']
s_city = cd['city']
if s_country: qs.append(Q(country__icontains = s_country))
if s_province: qs.append( Q(province__icontains=s_province))
if s_city: qs.append( Q(city__icontains=s_city))
f = None
for q in qs:
if f is None:
f=q
else: f &=q
list = UserProfile.objects.filter(f).order_by('-created_at')
else:
form = AdvancedSearchForm()
list = UserProfile.objects.all().order_by('-created_at')
paginator = Paginator(list,10)
page= request.GET.get('page')
try:
results = paginator.page(page)
except PageNotAnInteger:
results = paginator.page(1)
except EmptyPage:
results = paginator.page(paginator.num_pages)
args.update(csrf(request))
args['form'] = form
args['results'] = results
return render_to_response('userprofile/advanced_search.html', args,
context_instance=RequestContext(request))
urls.py部分是:
the urls.py part is:
url(r'^search/$', 'userprofile.views.profile_advanced_search'),
第一页的结果很好,但问题是当我转到第二页时,过滤后的结果就被遗忘了。
The results in the first page are fine but the problem is that when I go to the second page, the filtered results is just forgotten.
对于我来说,为什么这样发现:在分页使用GET时,在视图帐户中仅过滤POST,因此在第一页之后不会应用查询过滤器。
It is obvious to me why this happnes: filtering in the views accounts only for POST while pagination uses GET hence the queryset filter does not apply after the first page.
我查看了几个建议,并片段类似的问题,但没有一个足够接近我的意见,所以我无法弄清楚如何解决它,并感谢你的帮助。
I have looked at several suggestions and snippets for similar problem but none was close enough to my views, so I could not figure out how to fix it and appreciate your help.
更新:这里是相关的模板:
Update: here is the relevant template:
<form action="/search/" method="post">{% csrf_token %}
<ul class="list-unstyled">
<li><h3>Country</h3></li>
<li>{{form.country}}</li><br>
<h4>Province</h4>
<li>{{form.province}}</li>
<h4>City</h4>
<li>{{form.city}}</li>
</ul>
<input type="submit" name="submit" value="search" />
</form>
Search Results:
{% for p in results %}
<div">
<div>
<br>
<strong><a href="/profile/{{p.username}}" >{{p.username}}</a></strong>
{{p.country}} <br>
{{p.province}} <br>
{{p.city}} <br>
</div>
</div>
{% endfor %}
<div>
<div class="pagination">
{% if results.has_previous %}
<a href="?page={{ results.previous_page_number }}"> << Prev </a>  
{% endif %}
{% if results.has_next %}
<a href="?page={{ results.next_page_number }}"> Next >> </a>
{% endif %}
</div>
</div>
</div>
推荐答案
您应该转到发布/重定向/获取模式。您需要 2 视图,第一个为表单,第二个为结果。
You should move to Post/Redirect/Get pattern. You need 2 views, first one for form and second for results.
- 创建一个新的
url
能够解析所有三个参数加页面。/(P<国家> \w +)/(P<省> \w +)/(P<位点GT; \w +)/(P<网页> \d +)
。将此网址路由到新视图show_results
。这个视图显示了所有三个参数的页面
结果过滤。 - 在查看窗体中,在接收到搜索参数后,编写url并进行重定向给它
return HttpResponseRedirect(reverse('your.views.show_results',args =(country,province,site,0,)))
- Create a new
url
able to parse all three parameters plus page./(?P<country>\w+)/(?P<province>\w+)/(?P<site>\w+)/(?P<page>\d+)
. Route this url to a new viewshow_results
. This view showspage
results filtering for all three parameters. - In view form, after receive search parameters, compose url and make a redirect to it.
return HttpResponseRedirect(reverse('your.views.show_results', args=(country, province, site, 0, )))
显然,您可以使用 GET
键值参数来替代命名的url参数。
Obviously, instead to named url parameters you can work with GET
key value parameters.
看看谷歌如何以这种方式工作:
Take a look how google works in this way:
https://www.google.es/search? q = post + redirect + get
这篇关于Django分页过滤的搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!