tags:

views:

56

answers:

2

I'm designing a gallery application for viewing vehicle pictures and there are two parameters:

  1. Manufacturer
  2. Vehicle type

Right now you can view either, but not both. Urls go like so:

  1. /manufacturer/#
  2. /type/#

Where # is an ID number. How/can I format my URLs so it can accept both? My current solution is to do: /both/#/# but this requires some retooling since the application doesn't know when you want to filter by both. Any insight would be appreciated.

+1  A: 

If you're in a situation where both are optional, perhaps your best bet is to handle it through GET parameters.

digitaldreamer
Wow, that was exactly it. Thanks!
Parker
blokeley
Yes, they should be avoided when possible. However, GET parameters exist for a reason, and in some situations, at least in my opinion, it's ok to use them if it logically makes sense.
digitaldreamer
How else would I achieve this without resorting to very rigid URLs, where both parameters have to be specified? It seems silly to conform to a URL standard that makes your application harder to work with.
Parker
A: 

Probably that scheme will work for you:

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),
    ...

And depending on user’s way through the site, urls will be /manufacturer/123/, /manufacturer/123/456/ or /type/456/, /type/456/123/ (where 123 is a manufacturer id, and 456 is vehicle type id.)

Anatoly Rr