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

1

1 Answer 1

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

2
  • Thanks! but... I could not make this idea work for me. Mar 2, 2018 at 19:11
  • @GustavoAdolfo i didnt understand what this means: IndexView().by_category. Maybe you should change it to IndexView.as_view()
    – ndpu
    Mar 3, 2018 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.