Optional named groups Python re

Works this way to me:

r'^list_cv/(?:(?P<category>[\w+])/)?$'

EDIT:

Comparing to the original answer the difference is in the repetition match.

(?:(?P<category>[\w+])/)?$ vs original (?:(?P<category>[\w+])?/)$.


The last slash should be part of the optional RE, and the RE should be like

r'^list_cv/(?:(?P<category>[\w+])?/)$'

I didn't test it, though.


I find that it's more legible to create a separate url pattern for the url without the named group.

url(r'^list_cv/$', my_view),
url(r'^list_cv/(?P<category>[\d]+)/$', my_view),