wisemukiga wisemukiga - 1 year ago 79
Python Question

Django Regex matching long url

I have this long url and I want to match the string after

?
and have it available in self.kwargs of Class Based View.

new_timer/?UID=046F1572564080&runtime=1102&seconds=30stillrunning=1&numstoredtimes=3&storedtimes=13:2-23:32-48:43&checksum=71

I tried the following and it's not working.

Urlpatterns = [
# bunch of awesome urls
url(r'^new_timer/(?P<params>[^/]+)/$',NewTimerView.as_view(),
name='new_timer'),
]


What am I doing wrong?

Answer Source

What am I doing wrong?

Two mistakes in your regex: ^new_timer/(?P<params>[^/]+)/$

  1. You are not matching ? at all. Also you will have to escape it.

  2. You have / in end. Whereas there is no / in URL at end.

Correct regex should be: ^new_timer/\?(?P<params>[^/]+)$

Regex101 Demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download