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 trial

Python Django Basics Django Templates Add a Detail View

Why does the empty url have a chevron but not the others?

It's said in the video that for such URL patterns:

urlpatterns = [ url(r'^$', views.course_list), url(r'(?P<course_pk>\d+)/(?P<step_pk>\d+)/$', views.step_detail), url(r'(?P<pk>\d+)/$', views.course_detail), ]

The lower URLs don't have a chevron because in the base urls.py:

urlpatterns = [ url(r'^courses/', include('courses.urls')), url(r'^admin/', include(admin.site.urls)), url(r'^$', views.hello_world), ]

this indicates that the BEGINNING of the input should start with 'courses', and so anything in courses.urls isn't the beginning of the input, but right after courses/

so the course_pk would be after courses as such:

courses/course_pk

OK.

So why does the first URL in the courses.urls have a chevron?

To access the page, you would do courses/, right? So why a chevron? If a chevron is supposed to represent the BEGINNING of the input, and the empty string comes AFTER courses/, then why a chevron? Shouldn't it be:

url(r'$', views.course_list)

? The same logic for the other urls should extend to this one, no?

Jeff Muday
Jeff Muday
Treehouse Moderator 28,720 Points

You still need the chevron (caret) at the app level. Think of the project "consumes" the prefix, so whatever is left is sent down to find if a route exists in courses.urls.

"http://localhost:8000/courses/1/1" This is the URL typed into the browser.

"http://localhost:8000/" the "project URL" is consumed by Django

"courses/1/1" is interpreted in urls.py, and uses the route course.urls and consumes "courses/"

"1/1" is the part or the url string remaining and is routed by courses.urls and it finally finds the "$" termination character.

What's really "fun" is that when you decide to port your projects to the newly released Django 2.0, you have some new rules to learn. They're a little easier... less regular expression usage, a little more like the design of flask.

1 Answer

It should work with our without the ^. Django matches the first part of the string it only passes the remainder of the string to the included URL. if you dont include the ^ in your imported urls.py it will match anything that end with.

i.e. https://example.com/payments/pay/
would match on both cases, but
https://example.com/payments/lkjshdfpay/
would match only in the case you didn't include the ^

I don't like this behavior and always include the ^ as do the Django docs

With Django 2.0 on the horizion this is changing. Django 2.0 docs

payments/urls.py

urlpatterns = [
    url(r'^pay/$', views.payment_submit_view, name='pay'),
    or
    url(r'pay/$', views.payment_submit_view, name='pay'),
]

urls.py

urlpatterns = [
        url(r'^payments/', include('payments.urls', namespace='payments')),
]