What is the best way to format Django URLs for two parameters, any of which is optional?

I am developing a gallery application for viewing images on a car, and there are two options:

  • Manufacturer
  • Type of car

You can view both right now, but not both. Urls go like this:

  • /manufacturer/#
  • /a type/#

Where # is the identification number. How can I format my urls so that it can accept both? My current solution is: / both / # / #, but this requires some refurbishment, as the application does not know when you want to filter both. Any insight would be appreciated.

+3
source share
2 answers

, , , - GET.

+2

, :

views.py:

def filtered_view (request, manufacturer_id=None, type_id=None):
    ...

urls.py:

    ...
    url(r'^manufacturer/(?P<manufacturer_id>[0-9]+)/((?P<type_id>[0-9])/)?$', filtered_view),
    url(r'^type/(?P<type_id>[0-9]+)/((?P<manufacturer_id>[0-9])/)?$', filtered_view),
    ...

URL- /manufacturer/123/, /manufacturer/123/456/ /type/456/, /type/456/123/ ( 123 - , 456 - ).

0

Source: https://habr.com/ru/post/1739804/


All Articles