Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 ?

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,610 Points

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?

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,610 Points

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

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,610 Points

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!