python urlpattern newbie question [closed]
-
25-09-2019 - |
Question
I'm trying this pattern:
(r'^jobs/(?P<job_id>\d+)/$', job_handler)
To work with jobs/ and jobs/{job_id}, but the above expression doesn't cover the first case(jobs/), it only work if I do something like:
(r'^jobs/$', job_handler),
(r'^jobs/(?P<job_id>\d+)/$', job_handler)
Solution
You need to make the second part optional:
(r'^jobs/(?:(?P<job_id>\d+)/)?$', job_handler)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow