Answer the question
In order to leave comments, you need to log in
Can't build correct urls, where did I go wrong?
urlpatterns = patterns('',
url(r'^([\D\-]+)/$', views.category),
url(r'^[\w\-]+/(?P<slug>[\w\-]+)/$', views.product_page),
)
def category(request, slug):
slug = slug.split('/')[-1]
requested_category = get_object_or_404(Category, slug=slug)
def product_page(request, slug):
item = get_object_or_404(Product, slug=slug)
Answer the question
In order to leave comments, you need to log in
I once solved such a problem, but I never came up with a solution through urls.py.
In urlpatterns I had something like url(r'^catalog/.*\/$', catalog_view)
And already in the view I did path = re.split('/', PATH_INFO).
Further, the logic was as follows:
1) made a request for products by path[-1], if there was a result, filtered by the remaining parts of the path.
2) if nothing was found, then he assumed that we had a category in front of us and performed the appropriate actions.
3) if there are no categories, then 404.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question