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

Guy Scorpion
Guy Scorpion
11,053 Points

Django url - page not found

I have been doing the Django Basics course but running locally rather than in workspaces and am having some issues with app urls.

I am getting a 404 on my app url, but admin and root url are working fine.

learning_site/courses/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$ ', views.course_list),
]

learning_site/courses/views.py

from django.http import HttpResponse
from django.shortcuts import render

from .models import Course


def course_list(request):
    courses = Course.objects.all()
    output = ', '.join([str(course) for course in courses])
    return HttpResponse(output)

learning_site/learning_site/urls.py

from django.conf.urls import include, url
from django.contrib import admin

from . import views

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

learning_site/learning_site/views.py

from django.http import HttpResponse


def hello_world(request):
    return HttpResponse('Hello World')

the error I keep getting when viewing http://127.0.0.1:8000/courses

Using the URLconf defined in learning_site.urls, Django tried these URL patterns, in this order:
    1. ^courses/
    2. ^admin/
    3. ^$
The current URL, courses, didn't match any of these.

learning_site/learning_site/settings.py - Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'courses',
]

Really stuck with this. I've tried checking against code in workspaces and can't seem to see any differences. Surely I'm missing something simple.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

It might be as simple as the removing the extra trailing space in the regex.

urlpatterns = [
    url(r'^$', views.course_list),  # <-- removed trailing space
]
Guy Scorpion
Guy Scorpion
11,053 Points

ugh, how annoying. I must be blind, that was the issue. Thanks for picking that up, I was pulling my hair out.