如何破译 Django 中的动态 URL 魔法
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 sku 似乎不能包含斜杠,因此我建议使用“/”作为分隔符。然后就可以使用“.html”技巧了;事实证明,您的
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:Alternatively, always end your
collection_display
urls with a slash andproduct_display
with ".html" (or vice versa).