0

Hy all!

I'm new to python / django and I came across a problem that I can not solve. I have a route configured for the site's home (1) and a route configured for categories (2):

1) url(r'^$', IndexView().home, name='home')
2) url(r'^categoria/(?P<path>.*)/$', IndexView().by_category, name='by_category')

I need to set my home url to open a category by default, something like www.mysite.com/c=defaul_category

I tried in some ways, including: url (r '^ / (? P \ w +) / $', IndexView (). Home, name = 'home'). But I know it's incorrect.

So... I have no idea how to do this. Could someone help me? Thank you

GustavoAdolfo
  • 361
  • 1
  • 9
  • 23

1 Answers1

1

You should tell django that path in by_category url may be omitted. You have at least two options here:

1 - create one more url without path but with passed path variable as 3-rd argument in url:

url(r'^/(?P<c=vinhos>\w+)/$', IndexView().home, name='home')
url(r'^categoria/(?P<path>.*)/$', IndexView().by_category, name='by_category')
url(r'^categoria/$', IndexView().by_category,
    {'path': 'default_path'}, name='default_category')

2 - change regex pattern to make it possible to omit path parameter. Here | (or sign) added in the end of path group:

url(r'^categoria/(?P<path>.*|)/$', IndexView().by_category, name='by_category')

More about omitting url parameters Django optional url parameters

ndpu
  • 22,225
  • 6
  • 54
  • 69