views:

207

answers:

1
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'),

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

+1  A: 

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

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

eswald
The only problem with that is that I'm using this as my current <sku> regex:url(r'^([a-zA-Z0-9/_-]+)/(?P<sku>[a-zA-Z0-9_-]+)\.html$', 'product_display', name='product_display'),I have the ([a-zA-Z0-9/_-]+)/ at the beginning because the url could be anything at all as long as it has the somesku.html at the end and I want to be able to pull the sku out (from the dot back to the last slash). This because I want the following to be possible:http://mysite.com/collection/sub-collection/(matches a collection)http://mysite.com/collection/sub-collection/sku.html(matches a sku)
orokusaki
Isn't that precisely what the example answer does? So what is the problem?
eswald
Absolutely not. He's recommending the use of a slash as my delimiter. I cannot pass in the full path (including the slashes), up to the last slash, and use the slashes as my delimiter at the same time. Negative look-behind is the only way to achieve this, and the lack of support in frameworks is why so many web apps use colons and tildas in their urls.
orokusaki