Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't include url regexes in django check for end of string

Say you have a full url of localhost:thisdir/callview/

I have noticed that in a urls.py file, an included namespace is written as:

(r'^thisdir/', include('thisdir.urls', namespace='thisdir)),

where a beginning string is checked, but not end, and a view call is done as:

(r'^callview/$', 'thisdir.views.index', name='myview')

with the $ to check end of string. If the include pattern breaks "thisdir/" off the full url to check for that section first, and I thought it checks each string section at a time (so "thisdir/" is at the end of string) why do I never see (r'^thisdir/$', ...)

thank you

like image 866
codyc4321 Avatar asked Jan 09 '23 01:01

codyc4321


1 Answers

https://docs.djangoproject.com/en/1.8/topics/http/urls/

Note that the regular expressions in this example don’t have a $ (end-of-string match character) but do include a trailing slash. Whenever Django encounters include() (django.conf.urls.include()), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

If you terminated an include with a $, then the rule would no longer match anything in the included file, because it would only match URLs ending in exactly the include regex.

The reason you never see it is because it would prevent the purpose of a URL include.

(r'^thisdir/$', <include>) This would only match urls equal to thisdir/ due to the terminating $. Therefore a url such as thisdir/foobar/ will not match and never be processed by an include.

On the other hand, if you leave the $ out of the regex, /thisdir/<anything> will match the regex and therefore can be processed further by the included urls.

like image 198
Yuji 'Tomita' Tomita Avatar answered Jan 16 '23 20:01

Yuji 'Tomita' Tomita