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 Final Details Step Detail View

My URLs for the course steps work only for the first course I've created!

I can only access the first course I create from the browser when I type in the url, I deleted every course I'd made then re-created them but then also only the first course I create is accessible through the browser, that's when I type it in manually!

http://127.0.0.1:8000/courses/1/1 First Step http://127.0.0.1:8000/courses/1/2 Second Step http://127.0.0.1:8000/courses/1/3 Third Step

http://127.0.0.1:8000/courses/2/1 Doesn't work http://127.0.0.1:8000/courses/3/1 Doesn't work

After deletion and recreation:

http://127.0.0.1:8000/courses/4/1 Works no problem http://127.0.0.1:8000/courses/4/2 Works no problem

http://127.0.0.1:8000/courses/5/1 Doesn't work http://127.0.0.1:8000/courses/6/1 Doesn't work

Oguz Dumanoglu
Oguz Dumanoglu
5,169 Points

Can you include your urlpatterns from urls.py to here? I suppose there's a problem there.

4 Answers

Here's whats happening:

When we create a course it is automatically assigned a unique id.

So... If I create Course1, it will be given the ID of 1. If I then create Course2, it will be given the ID of 2. If I now browse to mysite/courses/1 I will see course1. If I browse to mysite/courses/2 I will see course2.

Ok easy enough. The same is happening when you create the steps. Each step you create goes into the Steps table of the database. So if I create Step1, Step2, Step3 all for course1, they will be assigned ID's of 1, 2 and 3. If I now create Step1 for course2, it's assigned the ID of 4. So to view it I'd have to goto mysite/course/2/4

This method of viewing the step's actually isn't very good at all. We should be creating a relationship between the courses and the steps in the database and using that to view the steps rather than using the ids.

Maybe Kenneth Love will go through that in a later stage.

Flore W
Flore W
4,744 Points

Hi Marc Busby great explanation, that's exactly what I am experiencing. How do you go about "resetting" the pk numbering for the various courses and steps? (especially for the ones deleted)

Max Hirsh
Max Hirsh
16,773 Points

It's hard to say what could be causing that without taking a look at your code. Can you paste some of what you've written so far?

Is it because you had records created with those primary key's which were subsequently deleted?

My understanding is that if a record is created with an auto-increment id, then if a record is deleted it will skip that id leaving you with gaps to show where records have been deleted.

So instead of seeing a sequential 1,2,3,4,5 you may see 1,2,4,5 etc

Not sure if that is your issue but it could be!

Juan Ignacio Beloqui
Juan Ignacio Beloqui
3,064 Points

i have the same question; heres my url's code:

from django.conf.urls import url from. import views

urlpatterns = [ url(r'^$', views.show_all_courses), url(r'(?P<course_pk>\d+)/(?P<step_pk>\d+)/$', views.step_detail), url(r'(?P<pk>\d+)/$', views.course_detail), ]