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 trialSahar Nasiri
7,454 PointsInclude
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),
1 Answer
Dariush Azimi
310 PointsIt 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),
]
Sergio Cruz
15,550 PointsSergio Cruz
15,550 PointsInclude 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.