如何破译 Django 中的动态 URL 魔法

发布于 2024-08-03 08:02:24 字数 518 浏览 6 评论 0原文

url(r'^([a-zA-Z0-9/_-]+):p:(?P<sku>[a-zA-Z0-9_-]+)/$', 'product_display', name='product_display'),
url(r'^(?P<path>[a-zA-Z0-9/_-]+)$', 'collection_display', name='collection_display'),

这是我当前的正则表达式:

我的问题是这样的:我希望能够匹配 Product_display 的正则表达式,而无需在正则表达式中使用 :p: 。我可以通过将 .html 放在末尾以将其与 collection_display 的正则表达式分开来做到这一点,但这并不能解决问题;如果正则表达式中没有“:p:”(如上面所示),URI“some-collection/other/other/sku.html”将匹配正则表达式一直到“.html”,而不管 sku。如何在不使用“:p:”结束集合正则表达式的情况下执行此操作。任何事情都会有所帮助。

谢谢

url(r'^([a-zA-Z0-9/_-]+):p:(?P<sku>[a-zA-Z0-9_-]+)/

That's my current regex:

My problem is this: I want to be able to match the product_display's regex without using :p: in the regex. I can do this by putting .html at the end to set it apart from the collection_display's regex, but that doesn't fix the problem that is; without the ":p:" in the regex as is above the URI "some-collection/other/other/sku.html" would match the regex all the way up to the ".html" disregarding the sku. How can I do this without using the ":p:" to end the collection regex. Anything will help.

Thanks

, 'product_display', name='product_display'), url(r'^(?P<path>[a-zA-Z0-9/_-]+)

That's my current regex:

My problem is this: I want to be able to match the product_display's regex without using :p: in the regex. I can do this by putting .html at the end to set it apart from the collection_display's regex, but that doesn't fix the problem that is; without the ":p:" in the regex as is above the URI "some-collection/other/other/sku.html" would match the regex all the way up to the ".html" disregarding the sku. How can I do this without using the ":p:" to end the collection regex. Anything will help.

Thanks

, 'collection_display', name='collection_display'),

That's my current regex:

My problem is this: I want to be able to match the product_display's regex without using :p: in the regex. I can do this by putting .html at the end to set it apart from the collection_display's regex, but that doesn't fix the problem that is; without the ":p:" in the regex as is above the URI "some-collection/other/other/sku.html" would match the regex all the way up to the ".html" disregarding the sku. How can I do this without using the ":p:" to end the collection regex. Anything will help.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

恬淡成诗 2024-08-10 08:02:24

您的 sku 似乎不能包含斜杠,因此我建议使用“/”作为分隔符。然后就可以使用“.html”技巧了;事实证明,您的 collection_display 正则表达式与点不匹配,但为了绝对确定,您可以使用负向后查找:

url(r'^([a-zA-Z0-9/_-]+)/(?P<sku>[a-zA-Z0-9_-]+)\.html

或者,始终结束您的 collection_display 网址带有斜杠,product_display 带有“.html”(反之亦然)。

, 'product_display', name='product_display'), url(r'^(?P<path>[a-zA-Z0-9/_-]+)(?<!\.html)

或者,始终结束您的 collection_display 网址带有斜杠,product_display 带有“.html”(反之亦然)。

, 'collection_display', name='collection_display'),

或者,始终结束您的 collection_display 网址带有斜杠,product_display 带有“.html”(反之亦然)。

It looks like your sku can't contain slashes, so I would recommend using "/" as your delimiter. Then the ".html" trick can be used; it turns out that your collection_display regex doesn't match the dot, but to make absolutely sure, you can use a negative look-behind:

url(r'^([a-zA-Z0-9/_-]+)/(?P<sku>[a-zA-Z0-9_-]+)\.html

Alternatively, always end your collection_display urls with a slash and product_display with ".html" (or vice versa).

, 'product_display', name='product_display'), url(r'^(?P<path>[a-zA-Z0-9/_-]+)(?<!\.html)

Alternatively, always end your collection_display urls with a slash and product_display with ".html" (or vice versa).

, 'collection_display', name='collection_display'),

Alternatively, always end your collection_display urls with a slash and product_display with ".html" (or vice versa).

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