D
D
Dmitry Sviridov2020-05-08 17:02:49
Django
Dmitry Sviridov, 2020-05-08 17:02:49

How to properly implement routing in Django?

The site has many pages like /<something 1>/<something 2>. The problem is that these "something 1" and "something 2" are in the database, but they can be completely different tables. For example:

/москва/ремонт-авто
/опель/колеса

The difficulty is that there are a lot of cities (they are stored in the database, they can change), there are a lot of car brands (they are stored in the database, they can change) - and all this lies in the database. That is, manually registering slugs of cities and car brands in the router is not an option. These 2 types of URLs (there can be many more like them) need to render completely different templates, because the types of these pages are different.
That is, to do it like this, of course, will not work, because these regular expressions are essentially the same:
url(r'^(?P<city_slug>.*)/(?P<service_slug>.*)/$', ServiceView.as_view(), name='service'),
url(r'^(?P<model_slug>.*)/(?P<part_slug>.*)/$', PartView.as_view(), name='part'),

I see these options:
  1. Use FlatPages and make a catchall-view that will retrieve the generated content of the flat page from the database by URL. It is not clear at what point to generate these pages
  2. Make a view, inside which to take a slug and render the desired template by its presence in a particular table. A url with a bunch of ifs or some kind of factory looks dubious
  3. Somehow inside the view, if the city slug is not found in the table, go to the next routing url. Not sure if this is feasible


How to implement this task in Django correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-05-08
@dimuska139

do with ids, as an example

/город-москва/ремонт-авто
/марка-опель/колеса

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question