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 Model Administration First App View

Sahar Nasiri
Sahar Nasiri
7,454 Points

Include

Why do we have to write include here?

url(r'^courses/', include('courses.urls'))

Why can't we just write :

url(r'^courses/', courses.urls) 

like the admin url right below it:

url(r'^admin/', admin.site.urls),
Sergio Cruz
Sergio Cruz
15,550 Points

Include is a built in function of Django that uses the string passed to it as a parameter ('courses.url') to build a file path to find the urls.py file inside of the courses folder. If you were to only write courses.urls, Django wouldn't know what that means. I'm guessing it would try to run as normal python code, which would be as checking the urls method on the courses object, and this would just throw an error.

1 Answer

It used to be the case in the previous version of django and it was changed in Django 1.9.

In previous versions (prior to 1.9), you would pass admin.site.urls to include(). It is not necessary to use include() anymore.

In this example, we register the default AdminSite instance django.contrib.admin.site at the URL /admin/

# urls.py
from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]