1

I would like to make a lists page with item such as /lists and have items such as /lists/node-js The url pattern I'm using is this -

url(r'^lists/(?P<foo>[\w\-]+)/$', views.lists_template, name='lists_template'), but due to this /lists does not work and shows a page not found error.How do I solve this?

1
  • Replace the + with *. Commented Jun 28, 2018 at 19:56

1 Answer 1

2

You use the wrong quantifier: in a regex, the + means one or more, whereas * means zero or more.

If you want to match the empty string as well, you thus need the * quantifier.

Furthermore we need to be able to make the last slash optional, since otherwise two slashes are required. So we can use the ? quantifier which means optional.

url(r'^lists/(?P[\w\-]*)/?$', views.lists_template, name='lists_template'),
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Got it :)

Your Answer

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