Sunday, August 26, 2012

how to “break” from a regex in urlconf

I have the following views:

def tag_page(request, tag):

    products
= Product.objects.filter(tag=tag)

   
return render(request, 'shop/tag_page.html', {'products': products, 'tag': tag})


def product_page(request, slug):

    product
= Product.objects.get(slug=slug)

   
return render(request, 'shop/product_page.html', {'product': product})

along with the following url configurations:

url(r'^(?P<tag>.+)/$', 'tag_page'),
url
(r'^(?P<tag>.+)/(?P<slug>[-\w]+)/$', 'product_page'),

The regex that has "tag" in it allows a url path to grow arbitrarily while sort of circularly redirecting to the tag_page view.

This lets me have the url: /mens/shirts/buttonups/, where all sections (/mens, /mens/shirts, /mens/shirts/buttonups/) of the path direct to the tag_page view, which is desired.

I want to end this behavior at some point however, and direct to a product_page view, which I attempt to accomplish with:

url(r'^(?P<tag>.+)/(?P<slug>[-\w]+)/$', 'product_page'),

When I follow a product_page link:

<a href="{{ product.slug }}">{{ product }}</a>

I am directed to the tag_pag view. Presumably because that slug url matches the tag regex.

So the question: Is there a way I can keep the flexible tag regex redirect behavior but then "break" from it once I reach a product page? One important thing to note is that I want to keep the product page within the built up url scheme like: mens/shirts/buttonups/shirt-product/

Any insight is appreciated, Thanks!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/EdbHp4taon0J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment