开发者

Django URL Pattern Matching Question

开发者 https://www.devze.com 2023-03-30 19:16 出处:网络
I have a problem I cant seem to figure out. Working in Django trying to get a hanlde on the URL matching . I have one that works. One that doesnt and I cannot figure out why. Hoping someone can help.

I have a problem I cant seem to figure out. Working in Django trying to get a hanlde on the URL matching . I have one that works. One that doesnt and I cannot figure out why. Hoping someone can help.

The one that works:

url(r'^person/device/program/oneday/(?P<meter_id>\d+)/$', 
therm_control.Get_One_Day_Of_Current_Device_Schedule.as_view(), 
name="one-day-url"),

When I use this url:

http://127.0.0.1:8000/personview/person/device/program/oneday/149778/
开发者_高级运维

The one that does not:

url(r'^person/device/program/oneday/(?P<meter_id>\d+)/(?P<day_of_the_week>\s+)/$', 
therm_control.Get_One_Day_Of_Current_Thermostat_Schedule.as_view(), 
name="one-day-url"),

When I use this url:

http://127.0.0.1:8000/personview/person/device/program/oneday/149778/Monday/

They view is the same in both cases. For trying the second I add the day_of_the_week to the def get()

What I see in the debug is the 404:

Request Method:     GET
Request URL:    http://127.0.0.1:8000/personview/person/device/program/oneday/149778/Monday/

I see this in the URLconf list so I know it is in the conf:

^personview/ ^person/device/program/oneday/(?P<meter_id>\d+)/(?P<day_of_the_week>\s+)/$ [name='one-day-url'] 

Any help is greatly appreciated.


The problem is that \s means a whitespace character in a regex, so in your URLconf the day of the week has to be a sequence of whitespace characters. If you replace \s with \w it will instead look for word characters, which is what you want.


\s+ will match one or more spaces, you want \w+:

url(r'^person/device/program/oneday/(?P<meter_id>\d+)/(?P<day_of_the_week>\w+)/$', 
    therm_control.Get_One_Day_Of_Current_Thermostat_Schedule.as_view(),
    name="one-day-url"),
0

精彩评论

暂无评论...
验证码 换一张
取 消