[Answer]-How to "break" from a regex in django urlconf

1👍

Do you really need the forward slash on the end of the product page URL? A URL which ends with a forward slash is distinct from one which does not.

Just like delimiters are left at the end of a path to suggest a directory (with files underneath it) and left off at the end of file paths, so too could you leave the slash on for the tag sections but lop it off for individual products.

That gets around the problem entirely 🙂

0👍

I think that you can’t do it with just urlconf- .* always matches everything.
I would do that in this way:

url(r'^(?P<path>.+)/$', 'path_page'),

def path_page(request,path):
    tags,unknown = path.rsplit('/',1)
    try:
        product = Product.objects.get(slug=unknown)
        return some_view_function(request,path,product)
    except Product.DoesNotExist:
        return some_another_view_function(request,path)

But- I see here a few problems:

  • What if tag has the same name as product’s slug?
  • Your solution is SEO unfriendly unless you want to bother with duplicate content meta tags
👤mrbox

Leave a comment