[Answer]-Hierarchical URLs in Django

1👍

Django URLs are just regular expressions, so the simplest way would be to just ignore everything prior to the “note” section of the URL. For example:

url(r'^.*/note(?P<note_id>[0-9]+)$', 'notes.view.show'),

However, this would ignore the book, chapter, paragraph components. Which would mean your notes would need unique ids across the system, not just within the book. If you needed to capture any number of the interim parts it would be more complicated.

I can’t confirm this will work right now, but using non-capture groups in regular expressions, you should be able to capture an optional book and chapter like so:

url(r'^(?:book(?P<book_id>[0-9]+)/)?(?:chapter(?P<chapter_id>[0-9]+)/)?note(?P<note_id>[0-9]+)$', 'notes.view.show'),

0👍

Use named groups to accomplish this: https://docs.djangoproject.com/en/dev/topics/http/urls/#named-groups

url(r'^book(?P<book_id>\d+)/chapter(?P<chapter_id>\d+)/section(?P<section_id>\d+)/paragraph(?P<paragraph_id>\d+)/note(?P<note_id>\d+)$', notes.view.show(book_id, chapter_id, section_id, paragraph_id, note_id)
👤davko

0👍

For those who really need a variable-depth URL structure and need the URL to consist strictly of slugs, not IDs, knowing all the components of the URL is critical to retrieve the correct record from the database. Then, the only solution I can think of is using:

 url(r'^.*/$', notes.views.show, name='show')

and then parsing the content of the URL to get the individual components after retrieving the URL in the view using the request.path call. This doesn’t sound ideal, but it is a way to accomplish it.

👤pkout

Leave a comment