views:

123

answers:

1

Hi,

I'm trying to create a filtered FAQ page in Django. It filters by three categories, and defaults to 'all' for all three when someone hits the root URL. From urls.py:

keywords = ('key1','key2','key3')
searchurl = r'^category1/(?P<%s>\w{1,50})/category2/(?P<%s>\w{1,50})/category3/(?P<%s>\w{1,50})/$' % keywords
searchall = dict(zip(keywords,['all']*len(keywords)))

urlpatterns = patterns('my.path.views',

    url(searchurl, 'faq', name='search_view'),
)

urlpatterns += patterns('django.views.generic.simple',
    url(r'^$', 'redirect_to', {'url': searchurl, 'kwargs': searchall}, name='default_search'),
)

This has all been working fine in my testing in Safari. However, when I tried it in Firefox, navigating to the root URL returned a Page Not Found error. It had re-directed to "root/^category1/(/", as if the regular expression had been passed as a URL and everything after the first ? was interpreted as a query string. Any idea what might be causing this?

Thanks!

+1  A: 

In your url pattern default_search, searchurl should be a url string, not a regular expression.

Looking at the Django docs on redirect_to, it looks like you can use string substitution from parameters captured from the url. You cannot substitute the searchall kwargs into the regex as you are trying. The following should work:

searchallurl = 'category1/all/category2/all/category3/all/'
url(r'^$', 'redirect_to', {'url': searchallurl,}, name='default_search'),

However if I understand your url config correctly, you don't need to redirect from the root url. Insead, call your faq view, with searchall as the optional dictionary:

url(r'^$', 'faq', searchall, name='default_search')
Alasdair
Correct on both counts. Thanks for your help, sir!
Dane