在 Django 中处理 URL

发布于 2024-08-13 20:36:54 字数 451 浏览 3 评论 0原文

所以,基本上我想做的是一个曲棍球池应用程序,并且我应该能够通过很多方法来过滤以查看数据。例如,按自由球员、目标、助攻、位置等进行过滤。

我计划使用一堆查询字符串来执行此操作,但我不确定传递这些查询字符串的最佳方法是什么。假设我想在第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只为守护你 2024-08-20 20:36:54

首先,考虑一下您是否真的想每次都保存所有参数。在您给出的示例中,您更改排序顺序但保留页码。考虑到您现在该页面上将有不同的元素,这真的有意义吗?更重要的是,如果您更改过滤器,当前选择的页码可能甚至不存在。

无论如何,假设这就是您想要的,您无需担心状态或 cookie 或任何其他内容,因为您需要的所有信息已经在 GET 参数中。您所需要做的就是根据需要替换这些参数之一,然后重新编码字符串。在模板标签中很容易做到,因为 GET 参数存储为 QueryDict,它基本上只是一个字典。

类似于(未经测试):

@register.simple_tag
def url_with_changed_parameter(request, param, value):
    params = request.GET
    request[param] = value
    return "%s?%s" % (request.path, params.urlencode())

您可以在模板中使用它:

{% url_with_changed_parameter request "page" 2 %}

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):

@register.simple_tag
def url_with_changed_parameter(request, param, value):
    params = request.GET
    request[param] = value
    return "%s?%s" % (request.path, params.urlencode())

and you would use it in your template:

{% url_with_changed_parameter request "page" 2 %}
云裳 2024-08-20 20:36:54

你看过 django-filter 吗?真是太棒了。

Have you looked at django-filter? It's really awesome.

没有伤那来痛 2024-08-20 20:36:54

检查管理应用程序中的过滤器机制,它包括使用查询字符串中提供的过滤器信息处理动态构造的 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.

陈独秀 2024-08-20 20:36:54

如果你想保存所有“参数”,我会说它们是资源标识符,通常应该是 URI 的一部分。

If You want to save all the "parameters", I'd say they are resource identifiers and should normally be the part of URI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文