同时使用排序和排序对查询集进行过滤
我有一个用户配置文件列表,我希望能够对其进行排序和过滤。
我已经能够通过手动输入 URL 来手动执行此操作,但我无法对模板页面进行编码以允许保留先前应用的过滤器或排序。这是我当前拥有的网址和模板代码 -
# in urls
url(r'^talent/filter\:(?P<position>[A-Za-z]+)/sort\:(?P<sort>[A-Za-z]+)$', 'talent_filter', name='talent_filter_sort'),
url(r'^talent/filter\:(?P<position>[A-Za-z]+)/$', 'talent_filter', name='talent_filter'),
url(r'^talent/sort\:(?P<sort>[A-Za-z]+)/$', 'talent_sort', name='talent_sort'),
url(r'^talent/$', 'talent', name='talent'),
# in template
<ul>
<li>SORT BY:</li>
<li><a href = "{% url talent_sort sort='alphabetical'%}">Alphabetical</a></li>
...
</ul>
<ul>
<li><a href = '{% url talent_filter position=position%}'>{{ position }}</a></li>
...
</ul>
目前,如果我在(未排序、未过滤的)人才页面上,并且我在结果上选择一个过滤器,它将返回 人才/过滤器:过滤器
。然后,如果我选择对结果进行排序,它(显然)会转到talent/sort:sort,删除之前的过滤器。
我想要完成的是,如果我当前在 talent/filter:filter
上并单击排序方法,它将转到 talent/filter:filter/sort:sort
,如果我已经对结果进行了排序 (talent/sort:sort
) 并单击筛选器,它也会将我带到 talent/filter:filter/sort:sort
。我将如何实现这一目标。谢谢。
I have a list of userprofiles that I want to be able to sort and filter.
I have been able to do this manually, by manually typing in the URLs, but I haven't been able to code the template page to allow the persistence of a previously-applied filter or sort. Here is the url and template code that I currently have --
# in urls
url(r'^talent/filter\:(?P<position>[A-Za-z]+)/sort\:(?P<sort>[A-Za-z]+)
Currently, if I am on the (unsorted, unfiltered) talent page, and I select a filter on the results, it will return
talent/filter:filter
. Then, if I choose to sort the results, it (obviously) goes to talent/sort:sort
, removing the previous filter.
What I want to accomplish is that if I am currently on talent/filter:filter
and click a sort method, it will go to talent/filter:filter/sort:sort
, and if I have already sorted the results (talent/sort:sort
) and click on filter, it will also take me to talent/filter:filter/sort:sort
. How would I accomplish this. Thank you.
, 'talent_filter', name='talent_filter_sort'),
url(r'^talent/filter\:(?P<position>[A-Za-z]+)/
Currently, if I am on the (unsorted, unfiltered) talent page, and I select a filter on the results, it will return
talent/filter:filter
. Then, if I choose to sort the results, it (obviously) goes to talent/sort:sort
, removing the previous filter.
What I want to accomplish is that if I am currently on talent/filter:filter
and click a sort method, it will go to talent/filter:filter/sort:sort
, and if I have already sorted the results (talent/sort:sort
) and click on filter, it will also take me to talent/filter:filter/sort:sort
. How would I accomplish this. Thank you.
, 'talent_filter', name='talent_filter'),
url(r'^talent/sort\:(?P<sort>[A-Za-z]+)/
Currently, if I am on the (unsorted, unfiltered) talent page, and I select a filter on the results, it will return
talent/filter:filter
. Then, if I choose to sort the results, it (obviously) goes to talent/sort:sort
, removing the previous filter.
What I want to accomplish is that if I am currently on talent/filter:filter
and click a sort method, it will go to talent/filter:filter/sort:sort
, and if I have already sorted the results (talent/sort:sort
) and click on filter, it will also take me to talent/filter:filter/sort:sort
. How would I accomplish this. Thank you.
, 'talent_sort', name='talent_sort'),
url(r'^talent/
Currently, if I am on the (unsorted, unfiltered) talent page, and I select a filter on the results, it will return
talent/filter:filter
. Then, if I choose to sort the results, it (obviously) goes to talent/sort:sort
, removing the previous filter.
What I want to accomplish is that if I am currently on talent/filter:filter
and click a sort method, it will go to talent/filter:filter/sort:sort
, and if I have already sorted the results (talent/sort:sort
) and click on filter, it will also take me to talent/filter:filter/sort:sort
. How would I accomplish this. Thank you.
, 'talent', name='talent'),
# in template
<ul>
<li>SORT BY:</li>
<li><a href = "{% url talent_sort sort='alphabetical'%}">Alphabetical</a></li>
...
</ul>
<ul>
<li><a href = '{% url talent_filter position=position%}'>{{ position }}</a></li>
...
</ul>
Currently, if I am on the (unsorted, unfiltered) talent page, and I select a filter on the results, it will returntalent/filter:filter
. Then, if I choose to sort the results, it (obviously) goes to talent/sort:sort
, removing the previous filter.
What I want to accomplish is that if I am currently on talent/filter:filter
and click a sort method, it will go to talent/filter:filter/sort:sort
, and if I have already sorted the results (talent/sort:sort
) and click on filter, it will also take me to talent/filter:filter/sort:sort
. How would I accomplish this. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为实现此目的的一种方法是在会话中存储一个标志,指示应该进行什么排序或过滤。例如,可以使用如下所示的内容来保存排序选择的状态。
然后您的视图可以检查会话密钥是否存在并相应地应用过滤器。
然后,排序和过滤可以在请求之间持续进行。
如果您想删除排序或过滤器,那么也许您可以根据某些用户操作删除视图中的会话键:
此外,根据您的要求,您可能会考虑将 2 个 url 合并为 1 个,然后仅使用 GET 参数来激活那种。
这些例子可能需要更严格一些,但这就是想法。希望这有帮助。
乔
I think one way you might be able to accomplish this is store a flag in your session that indicates what the sorting or filtering should be. For example, something like below could be used to save the state of the your sort choice.
And then your views can check for the existence the session keys and apply a filter accordingly.
The sorting and filtering could then persist across requests.
If you want to remove the sort or filter, then perhaps you can delete the session keys in the view depending on some user action:
Also, depending on your requirements, you might consider combining the 2 urls into 1 and just use GET parameters to activate the sort.
These examples could probably use some more rigor, but that's the idea. Hope this helps.
Joe
尽管有点违背 django 的全部内容,但如果使用 GET 变量,则用户指定排序/过滤器的最佳方法。因此,您的 URL 将显示为:
/talent/?filter=foo&sort=bar
在您的视图中,为当前过滤器设置上下文变量并排序,然后使用这些变量在模板中构建您的 URL。
像这样的东西:
按字母顺序排列
如果您确实觉得有必要使用 url 捕获的参数,则需要设置 url 来处理所有条件(排序集、过滤器集、两者都集和两者集)。然后在您的模板中,您将需要一堆 if 语句来选择正确的 url 和参数。
正如我所说,使用 GET 参数可以更好地处理这种情况。
Despite being slightly against what django is all about, the best approach to user specified sorts/filters if to use GET variables. So your URLs would appear like:
/talent/?filter=foo&sort=bar
Within your view set context variables for your current filter and sort then use those to build your urls in your templates.
Something like:
Alphabetical
If you really feel it's necessary to use url captured parameters you'll need to set up your urls to handle all the conditions (sort set, filter set, neither set, and both set). And then in your templates you'll need a bunch of if statements to select the correct url and parameters.
As I said this type of situation is much better handled with GET parameters.