S
S
Stanislav2019-05-28 17:59:24
Django
Stanislav, 2019-05-28 17:59:24

How to arrange routing in django?

Greetings!
How to organize routing in Django?
There is a table with categories (id, name, parent_id, slug) and a table with pages (id, name, category_id, slug).
Categories have an arbitrary level of nesting. When forming a link, something like this is obtained: site.ru/category/category-lvl-1/category-lvl2/some-page
Actions:

  1. Parse url by / . Get list('category', 'category-lvl-1', 'category-lvl2', 'some-page')
  2. On the last element of the list, make a query to the page table.
    1. If there is, get the page with that slug. For the rest, make a selection from the categories.
    2. If there is no such page, make a selection for all elements from the categories.

  3. If there are such categories, match the order by parent_id.
  4. If there are no errors, display the category/page, otherwise - 404.

Do I understand the order of actions correctly, or is there a way to make it easier in Django?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-05-28
@Chebaa

Although somewhat cheating, but an extremely simple solution:

urlpatterns = [
    url(r'^(?P<slug>[-\w/]+)/details/$', ProductDetail.as_view(), name='product'),
    url(r'^(?P<slug>[-\w/]+)/$', ProductList.as_view(), name='category'),
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question