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 trialCraig Campbell
14,428 Pointswhy is django include() not working?
I am following along with the django basics course, and everything works until I try to mount a view at /courses using include():
urlpatterns = [
url(r'^courses/', include('courses.urls')),
url(r'^admin/', admin.site.urls),
url(r'^$', views.hello_world),
]
Now when I runserver, I get NameError 'include' is not defined.
I had been using django1.9 but i uninstalled it and installed 1.8 to be in sync with the course. but same error. Now instead of an error not letting the server go up, the server lets me go to the site where it THEN tells me about the error (in django 1.9 i couln't get the server up).
Now if I take out the courses/ route and the include() then it runs fine in both 1.8 and 1.9.
Ideas?
4 Answers
Craig Campbell
14,428 Pointswow thanks. Didn't think of that since I don't recall seeing that in the video. Should have been obvious! Thanks!!
Gavin Ralston
28,770 PointsI'm following along with Django 1.9.x and it looks a little different. He didn't have to manually type the 'include' import because it appears to have been generated automatically, but the urls.py appeared just slightly different for me by default (barely, but that import statement was the problem)
from django.conf.urls import url, include # include wasn't part of the generated import statement
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^courses/', include('courses.urls')),
url(r'^admin/', admin.site.urls), # this isn't sent in to the include() function.
url(r'^$', views.hello_world),
]
Jennifer Liu
1,636 PointsHi, I am on the same problem. For me, when I try to run server on port 8000, it wouldn't let me because it says 'courses' not found. This is my code, and I don't see what is wrong with that.
from django.conf.urls import include, url
from django.contrib import admin
from courses import views
from . import views
urlpatterns = [
url(r'^courses/',include(courses.urls)),
url(r'^admin/', include(admin.site.urls)),
url(r'^$',views.hello_world),
]
Chris Freeman
Treehouse Moderator 68,457 PointsTry changing "from courses import views
"
To "import courses
"
The last import line overrides the import of views
from courses
anyway.
Daniel Henderson
6,768 Pointsinclude('courses.urls')),
the thing to include needs to be a string, and he says django will take this to mean a path. I guess django has a special way to indicate a compact, cross-platform path to a file, but it's got to be in quotes.
Jennifer Liu
1,636 PointsHi, so that worked, but now I am getting another error. It says module courses' has no attribute 'urls' But in my courses, I do have a url.py and in there I added
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.course_list),
]
Daniel Petrov
3,495 PointsYou have to put courses.urls in quotes
url(r'^courses/',include('courses.urls')),
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 PointsKyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 PointsChris is right here. Kenneth is using an older version of Django. So the from django.conf.urls import include is already there. In newer versions of Django, the include admin is not required anymore so django does not already have the import include part. So all you need to do to fix this in newer versions is just add the import of include there.