Django: variable parameters in URLconf

百般思念 提交于 2019-12-05 12:22:27

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

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?

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!