Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Django REST Framework Make the REST Framework Work for You Overriding Generic View Methods

When 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
hamsternation
26,613 Points

The 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! :)