D
D
daho0052020-06-15 14:36:31
Django
daho005, 2020-06-15 14:36:31

How to do infinite link deepening in Django?

A friend has a website where you can create a directory in the admin panel, and there is another directory in it, and there is another in it, and so on.
I came up with this thing: a model, with reference to itself. You can add an entry, then create another entry and link it to the first one.
Let's say I want to add an Animals entry, then a Mammals entry and link it to the first one, then add a Feline entry and link it to Mammals, and the last entry is Tiger, which will be linked to the Feline family. And the trick is that such recesses can be done endlessly.
Question. How can I make such a tree in urls.py so that it looks the same in the address bar as it does on a friend's site?:

http://localhost/Jivotnie/Mlekopitaushie/Semeistvo_Koshachih/Tigr/и так далее

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Sobolev, 2020-06-15
@daho005

You definitely won’t be able to do it endlessly, because.


The maximum length of a URL in its address bar is 2048 characters

If we are talking about hierarchical structure, then you should pay attention to the mptt library
https://django-mptt.readthedocs.io/en/latest/
To make an infinite url, just describe the regular expression corresponding to the address as (.+)
url(r"^(?P<MyStrangeVariable>\d+)/$", MyStrangeCode.as_view()),

And then the resulting value from the address string can be split using split('/')
I will make a reservation right away, this is not the best solution. Read about regular expressions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question