(r'^section(?:/(?P<id>\w+))?/$', section)
Notice the last ?
to make the whole (?:/(?P<id>\w+))
optional.
Other answers are missing the ?
or they don't make one of the slashes (/
) optional like I do with the first one in (?:/(
...
r'^section/(?:(?P<id>\w+)/)?$' # same result, last '/' optional.
And make the parameter optional in the function as well:
def section(request, id=None):
# ...