A
A
Animkim2016-05-15 17:26:31
Django
Animkim, 2016-05-15 17:26:31

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

views
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)

Now, as you can see, I distinguish the url of the categories from the url of the product in a very strange way, there cannot be numbers in the category (in fact, it can), but the product must have a number.
url categories up to three levels:
/cat1/cat2/cat3/product
/cat1/cat2/product
/cat1/product
So that's the question itself, where did I turn the wrong way and how to get out to the light?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2016-05-15
@Animkim

Example:
url(r'^new/(?P[\w-]+)$', news.new, name="new"),

T
tema_sun, 2016-05-15
@tema_sun

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 question

Ask a Question

731 491 924 answers to any question