在 Django 中处理 URL
所以,基本上我想做的是一个曲棍球池应用程序,并且我应该能够通过很多方法来过滤以查看数据。例如,按自由球员、目标、助攻、位置等进行过滤。
我计划使用一堆查询字符串来执行此操作,但我不确定传递这些查询字符串的最佳方法是什么。假设我想在第 2 页上(因为我使用分页来分割页面),按目标排序,并且只显示向前,我将设置以下查询:
?page=2&sort=g&position=f
但是如果我在该页面上,并且它是向我显示所有这些相应的信息,如果我单击“点”而不是“目标”,我仍然希望所有其他过滤器完好无损,所以像这样:
?page=2&sort=p&position=f
由于 HTTP 是无状态的,因此我很难找到最好的方法这将是..如果有人有一些好主意,他们将不胜感激,谢谢;)
Shawn J
So, basically what I'm trying to do is a hockey pool application, and there are a ton of ways I should be able to filter to view the data. For example, filter by free agent, goals, assists, position, etc.
I'm planning on doing this with a bunch of query strings, but I'm not sure what the best approach would be to pass along the these query strings. Lets say I wanted to be on page 2 (as I'm using pagination for splitting the pages), sort by goals, and only show forwards, I would have the following query set:
?page=2&sort=g&position=f
But if I was on that page, and it was showing me all this corresponding info, if I was to click say, points instead of goals, I would still want all my other filters in tact, so like this:
?page=2&sort=p&position=f
Since HTTP is stateless, I'm having trouble on what the best approach to this would be.. If anyone has some good ideas they would be much appreciated, thanks ;)
Shawn J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,考虑一下您是否真的想每次都保存所有参数。在您给出的示例中,您更改排序顺序但保留页码。考虑到您现在该页面上将有不同的元素,这真的有意义吗?更重要的是,如果您更改过滤器,当前选择的页码可能甚至不存在。
无论如何,假设这就是您想要的,您无需担心状态或 cookie 或任何其他内容,因为您需要的所有信息已经在 GET 参数中。您所需要做的就是根据需要替换这些参数之一,然后重新编码字符串。在模板标签中很容易做到,因为 GET 参数存储为 QueryDict,它基本上只是一个字典。
类似于(未经测试):
您可以在模板中使用它:
Firstly, think about whether you really want to save all the parameters each time. In the example you give, you change the sort order but preserve the page number. Does this really make sense, considering you will now have different elements on that page. Even more, if you change the filters, the currently selected page number might not even exist.
Anyway, assuming that is what you want, you don't need to worry about state or cookies or any of that, seeing as all the information you need is already in the GET parameters. All you need to do is to replace one of these parameters as required, then re-encode the string. Easy to do in a template tag, since GET parameters are stored as a QueryDict which is basically just a dictionary.
Something like (untested):
and you would use it in your template:
你看过 django-filter 吗?真是太棒了。
Have you looked at django-filter? It's really awesome.
检查管理应用程序中的过滤器机制,它包括使用查询字符串中提供的过滤器信息处理动态构造的 URL。
另外 - 考虑将实际状态信息保存在 cookie/会话中。
Check out filter mechanism in the admin application, it includes dealing with dynamically constructed URLs with filter information supplied in the query string.
In addition - consider saving actual state information in cookies/sessions.
如果你想保存所有“参数”,我会说它们是资源标识符,通常应该是 URI 的一部分。
If You want to save all the "parameters", I'd say they are resource identifiers and should normally be the part of URI.