Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHussein Almutawa
12,313 PointsWhen to use the ^ on urls.py and when not to?
So I noticed that in the urls.py file of the "Overriding Generic View Methods" workspaces that the 'course_detail' URL does not use a ^. All other URLs use a ^.
*So I have the following questions: 1) When is using a ^ necessary, and why use it? 2) Does order of URLs matter in the urlpatterns list?
*Please note that I have already taken the RegEx Python class, but can't seem to find the answer to those questions. Thank you!
2 Answers
hamsternation
26,616 PointsThe carat expression in regex represents where you want the search to start. the $ symbol is where you want the search to end.
for example, for a url(r'course', views.hello_world)
entry in the urlpatterns, anything that has the word "course" in it as an url would direct you to the "hello_world" page... i.e.
mysite.com/somerandomcoursehere
mysite.com/courewaresforyou
mysite.com/course
mysite.com/ofcourse
adding the ^ and $ limits the regex to the characters in between, so url(r'^course/', views.hello_world)
matches only when the user types mysite.com/course, which is what you want most of the time.
I hope that answers your question! :)
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Hussein,
Further to hamsternation 's answer, the order of URLs does matter: the first match found in the pattern list will be used.
Cheers,
Alex