在研究了这个问题一段时间并从这个条目开始之后,我最终得到了一个复杂但通用的可重用解决方案。
对 OP 来说太过分了,但很多人似乎都有分页和搜索问题,所以这是我的 0.02 美元。
(对于 Django 来说有点新手,所以我确信有一些需要改进的地方,但是我现在能够很快地将搜索和分页结合起来。请忽略任何关于 'db' 或 'mdb' 的内容,这是高度特定于我的应用程序经常在 Django 数据库之外执行原始 SQL。)
概要:
基本上,我认为我可以用一块石头杀死两只鸟。我处理过滤行集的原因是因为我正在从表单中进行搜索。
而且...该表单非常有能力提供重建分页 URL 所需的信息。
因此,基本上最终得到了一个系统,该系统的工作主要包括构建一个普通的搜索表单,然后将其与 ListView 的适当子类化版本连接起来。请参阅类 RoleListView。
我还需要创建列表视图的模板,但是如果您参考它,@ pssecurity/security_list.html 您会发现它非常基本。
细节:
关于分页的聪明之处在于 KSearchListView(ListView) 类。这些东西是完全通用的,我可以在尽可能多的搜索页面上重复使用它。
方法 get_queryset,其中通过调用 form.doSearch 进行数据库过滤。
实际的分页在方法 get_context_data 上,该方法检查是否存在表单,是否有效,然后通过使用表单的已清理参数重新填充它来操作分页 URL。
另请注意,有两个传入的 URL,一个未过滤的列表,一个过滤的搜索)。两者都映射到同一个 ListView。
urls
#Roles aka PSROLEDEFN
url(r'^roles/search',
login_required(RoleListView.as_view()),
name="psroledefn_search"),
url(r'^roles/$',
# 'pssecurity.views.psroledefn_list',
login_required(RoleListView.as_view()),
name="psroledefn_list"),
#generic
class KSearchListView(ListView):
def __str__(self):
return self.__class__.__name__
__repr__ = __str__
form_class = form = None
di_hardcoded_context = {}
def setdb(self):
#specific to my app
self.db = self.rdb
def dispatch(self,*args,**kwargs):
#specific to my app
if not self.mdb:
self.mdb = MultiDb.get(self.kwargs["dbr"])
self.djangodb = self.mdb.djangodb
self.rdb = self.mdb.rdb
self.setdb()
#specific to my app
return super(KSearchListView, self).dispatch(*args,**kwargs)
def get_queryset(self,*args,**kwargs):
# logging.info("%s.get_queryset(%s,%s)" % (self,args,kwargs))
self.request.get = self.request.GET
if self.form_class:
#pagination info
#do we have a form and are we coming from it?
if self.request.method == "GET":
self.form = self.form_class(self.db, self.request.GET)
if self.form.is_valid():
logging.info("form.doSearch")
li_c = self.form.doSearch()
return li_c
else:
logging.debug("not is_valid branch")
else:
self.form = self.form_class(self.mdb.rdb)
#fetch all rows for the underlying table
return self.fetch_all()
def fetch_all(self):
#specific to my app
#you would probably use a <model>.objects.all()
return list(pssys.Pssys.fetch(self.db,self.recname))
def _override_context_data(self,context):
pass
def get_context_data(self,*args,**kwargs):
# logging.info("%s.get_context_data(%s,%s)" % (self,args,kwargs))
context = super(KSearchListView, self).get_context_data(**kwargs)
context['form'] = self.form
context["dbr"] = self.mdb.rdbname
#pagination info
#we are going to put the GET variables right back into the next/prev
url = ""
page_obj = context["page_obj"]
if self.form and self.form.is_valid() and self.form.cleaned_data:
li = [self.request.path,"?"]
#driving the url assembly from the form fields and
#using the cleaned data should be pretty safe
di = self.form.cleaned_data
for k in self.form.fields:
li.append("%s=%s" % (k,di[k]))
li.append("&")
# li = li[:-1]
url = "".join(li)
#if we didn't come in through a search form
if not url:
url = "?"
#now conditionally add the previous/next as appropriate.
#url has either a trailing ? or & at this point
kpaging_previous_url = kpaging_next_url = ""
if page_obj.has_previous():
kpaging_previous_url = context["kpaging_previous_url"] = url + "page=%s" % (page_obj.previous_page_number())
if page_obj.has_next():
kpaging_next_url = context["kpaging_next_url"] = url + "page=%s" % (page_obj.next_page_number())
logging.debug("url:%s" % (url))
logging.debug("kpaging_previous_url:%s" % (kpaging_previous_url))
logging.debug("kpaging_next_url:%s" % (kpaging_next_url))
#pickup anything the subclass has set for the context
context.update(self.di_hardcoded_context)
self._override_context_data(context)
return context
#what I need to write for every list/search page...
class RoleListView(KSearchListView):
template_name = "pssecurity/security_list.html"
paginate_by = 20
recname = "PSROLEDEFN"
form_class = Search_PSROLEDEFN
di_hardcoded_context = dict(
title="Search Roles",
header="Roles",
templatename_inst="PSROLEDEFN_list",
url_action='security:psroledefn_search')
#pagination info the forms
#generic
class SearchForm(forms.Form):
def __init__(self, db, request=None):
self.db = db
li_arg = [request] if request else []
super(forms.Form, self).__init__(*li_arg)
def __str__(self):
return self.__class__.__name__
__repr__ = __str__
#what I need to write for every list/search page...
class Search_PSROLEDEFN(SearchForm):
ROLENAME = forms.CharField(max_length=20, required=False)
DESCR = forms.CharField(max_length=32, required=False)
status_custom = ChooseStatusCustomizationField()
hasUsers = ChooseYesNoDontCareField("has Users?")
hasPermissions = ChooseYesNoDontCareField("has Permissions?")
hasPortalReferences = ChooseYesNoDontCareField("has Portal?")
def doSearch(self):
ROLENAME = self.cleaned_data["ROLENAME"]
DESCR = self.cleaned_data["DESCR"].strip()
status_custom = self.cleaned_data["status_custom"]
hasPortalReferences = self.cleaned_data["hasPortalReferences"]
hasPermissions = self.cleaned_data["hasPermissions"]
hasUsers = self.cleaned_data["hasUsers"]
#cut out a lot of code specific to my app
#you would want to do an appropriate
#<model>.objects.filter()...
returns <select from my db>
#a typical template, note that isn't even specific to an object
#refer to class RoleListView to see how the template is built.
#the actual details of the fetched objects are left to <templatename_inst>
pssecurity/security_list.html
{% block search %}
<div id="block_search">
<span>{{header}}</span>
<div class="row">
{% if form %}
<div id="search" class="well col-xs-9" >
<form class= "form-horizontal" action="{% url url_action dbr=dbr %}" method="get">
{{form.as_table}}
<input type="submit" value="search">
</form>
</div>
{% endif %}
{% endblock %}
{%block content %}
<div id = "block_content">
{% for inst in object_list %}
<div class="row">
{% include templatename_inst %}
</div>
{% endfor %}
{% include "websec/kpagination.html" %}
</div>
{%endblock %}
#generic
kpagination.html
<div class="pagination">
<span class="step-links" >
{% if li_inst.has_previous %}
<a href="?page={{ li_inst.previous_page_number }}">previous</a>
{% endif %}
<span class="current" >
Page {{ li_inst.number }} of {{ li_inst.paginator.num_pages }}.
</span>
{% if li_inst.has_next %}
<a \href="?page={{ li_inst.next_page_number }}">next</a>
{% endif %}
</span>
</div>