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

/admin/courses/course/1/ being rerouted to /admin/courses/course/1/change/

I've tried to exactly follow the video, but at the end of this video when I attempt to go to /admin/courses/course/1/ as Kenneth does I get automatically rerouted to /admin/courses/course/1/change/ instead.

Django version 1.11.5, using settings 'learning_site.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[17/Sep/2017 14:34:00] "GET /admin/courses/course/1/ HTTP/1.1" 302 0
[17/Sep/2017 14:34:00] "GET /admin/courses/course/1/change/ HTTP/1.1" 200 15543

Here are the relevant files (I hope):

# learning_site/learning_site/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from . import views

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

urlpatterns += staticfiles_urlpatterns()
# learning_site/courses/urls.py
from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.course_list),
    url(r'(?P<pk>\d+)/$', views.course_detail),
]
# learning_site/courses/views.py
from django.shortcuts import render
from .models import Course

def course_list(request):
    courses = Course.objects.all()
    return render(request, 'courses/course_list.html', {'courses': courses})

def course_detail(request, pk): # pk is primary key
    course = Course.objects.get(pk=pk)
    return render(request, 'courses/course_detail.html', {'course': course})
# learning_site/courses/admin.py
from django.shortcuts import render

from .models import Course


def course_list(request):
    courses = Course.objects.all()
    return render(request, 'courses/course_list.html', {'courses': courses})

def course_detail(request, pk): # pk is primary key
    course = Course.objects.get(pk=pk)
    return render(request, 'courses/course_detail.html', {'course': course})

ryan s ?

Are there code snippets missing?

I think you missed the views.py and urls.py for the course change. Looks like course_list and course_detail are there.

Hi Chris- Isn't the 'change" a function of the django included admin model, and not my app model? Not sure what you mean, can you elaborate?

Yes, admin urls have a change url. I just realized your server output was of you going into the admin.

But in this video Kenneth is not going into the admin to look at his courses views.

HI Chris- thanks for responding! But I'm confused... isn't he going to the same url as I? /admin/courses/course/1/ ?

1 Answer

So notice how each GET request starts with /admin

[17/Sep/2017 14:34:00] "GET /admin/courses/course/1/ HTTP/1.1" 302 0
[17/Sep/2017 14:34:00] "GET /admin/courses/course/1/change/ HTTP/1.1" 200 15543
urlpatterns = [
    # any url starting with /courses handled by this apps urls.py
    # so this would be: http://localhost/courses
    url(r'^courses/', include('courses.urls', namespace='courses')),

    #any url starting with /admin handled by this apps urls.py
    # and this would be http://localhost/admin
    url(r'^admin/', admin.site.urls),

    # Anything that matches nothing at the end.
    # and this: http://localhost
    url(r'^$', views.homepage),
]

localhost = 127.0.0.1

So you should be making requests against

http://localhost:8000/courses aka http://127.0.0.1:8000/courses

right now you are requesting

http://127.0.0.1:8000/admin/courses so just remove /admin out of the URL.

OH! Gawd... sorry, not sure how I got that admin into the url... Thank you for correcting me!

No worries!

Little mistakes get the best of us all.