4

I've been looking for this question and couldn't find any, sorry if it's duplicated.

I'm building some kind of ecommerce site, similar to ebay. The problem i have arise when i'm trying to browse through "categories" and "filters". For example. You can browse the "Monitor" category. That will show you lots of monitors, and some filters (exactly the same as ebay) to apply them. So, you go to "monitors", then you have filters like:

  • Type: LCD - LED - CRT
  • Brand: ViewSonic - LG - Samsung
  • Max Resolution: 800x600 - 1024x768

And those filters will be appended to the URL, following with the example, when you browse monitors the URL could be something like:

store.com/monitors

If you apply the "Type" filter:

store.com/monitors/LCD

"Brand":

store.com/monitors/LCD/LG

"Max Resolution":

store.com/monitors/LCD/LG/1024x768

So, summarizing, the URL structure would be something like:

/category/filter1/filter2/filter3

I can't figure out how to do it really. The problem is that filters can be variable. I think in the view will need to use **kwargs but i'm not really sure.

Do you have any idea how to capture that kind of parameters?

Thanks a lot!

santiagobasulto
  • 11,320
  • 11
  • 64
  • 88
  • do your filters have a defined order? or are both `/LG/LCD` and `/LCD/LG` valid? – second Dec 08 '11 at 14:21
  • No, doesn't have an order. Think about that. You could browse Monitors and be interested in check LCD and other guy may want to filter by Brand. – santiagobasulto Dec 08 '11 at 15:18
  • If it doesn't have an order, how are you expecting any code to know that "LCD" is a type, rather than a brand? – Ben Dec 08 '11 at 15:28
  • Maybe the brand was not the best example. After the category parameter, all other params following are "filters", represented with a "Filter" model and a many-to-many with Product. The relationships are complicated. Anyway, my question is about how to capture those parameters. Trust me, the design is OK. Thanks! – santiagobasulto Dec 08 '11 at 15:32

3 Answers3

3

Ben, I hope this will help you

urls.py

from catalog.views import catalog_products_view

urlpatterns = patterns(
    '',
    url(r'^(?P<category>[\w-]+)/$', catalog_products_view, name="catalog_products_view"),
    url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/$', catalog_products_view, name="catalog_products_view"),
    url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/(?P<filter2>[\w-]+)/$', catalog_products_view, name="catalog_products_view"),
    url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/(?P<filter2>[\w-]+)/(?P<filter3>[\w-]+)/$', catalog_products_view, name="catalog_products_view"),
)

view.py

def catalog_products_view(request, category, filter1=None, filter2=None, filter3=None):
    # some code here

or

def catalog_products_view(request, category, **kwargs):
    filter1 = kwargs['filter1']
    filter2 = kwargs['filter2']
    ....
    filterN = kwargs['filterN']
    # some code here
Alexey Savanovich
  • 1,893
  • 11
  • 19
  • That's exactly what i want! But, is there any way to avoid the hardcoding of the URLs? I mean, in url.py you're explicitly putting filters, and that's what will be variable. – santiagobasulto Dec 09 '11 at 10:52
  • To avoid hardcoding you can use regexp `r'^(?P[\w-]+)/(?P[\w\/]+)/$` and in view split `complex_filter` by '/' – Alexey Savanovich Dec 09 '11 at 11:14
1

You could add this to your urls:

url(r'^(?P<category>\w)/(?P<filters>.*)/$', 'myview'),

And then myview would get the parameters of category and filters. You could split filters on "/" and search for each part within the Filters table.

Does that make sense?

Ben
  • 6,687
  • 2
  • 33
  • 46
0

how do you intend to decide what aspect is being filtered by? Do you have a list of accepted keywords for each category? ie how does the server know that

/LCD/LG/

means type=LCD, brand=LG

but

/LG/LCD

doesn't mean type=LG, brand=LCD etc

Is there any reason you don't want to use GET params, e.g.

.../search/?make=LD&size=42
second
  • 28,029
  • 7
  • 75
  • 76
  • Yes, i'm supposing that have an unique String representation. I could do /13-LCD/18-LG and have the ID ... Those are filters, and reside in a single model Filter, so it woudln't be problems. About the GET params. I already thought it, and don't like. I can also parse the URL, i mean, it's simple. The first param is the category and the others are filters. But, i was thinking that with some feature of URLconf i could get a list, or something. Thank you for your answer! – santiagobasulto Dec 08 '11 at 15:29