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

Kade Carlson
Kade Carlson
5,928 Points

Not sure why it is giving me a name error

Below is my code for views, admin, models, and urls and every time I try to run the server it throws a name error and I am not sure why. Here is the error...

NameError at /courses/1/2/ name 'Step' is not defined

VIEWS

from django.shortcuts import get_object_or_404, render
from .models import Course 


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

def course_detail(request, pk):
    course = get_object_or_404(Course, pk=pk)
    return render(request, 'courses/course_detail.html', {'course': course})

def step_detail(request, course_pk, step_pk):
    step = get_object_or_404(Step, course_id=course_pk, pk=step_pk)
    return render(request, 'courses/step_detail.html', {'step': step})

ADMIN

from django.contrib import admin

# Register your models here.
from .models import Course, Step

class StepInline(admin.StackedInline):
    model = Step


class CourseAdmin(admin.ModelAdmin):
    inlines = [StepInline,]

admin.site.register(Course, CourseAdmin)
admin.site.register(Step)

MODELS

from django.db import models

# Create your models here.
class Course(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=255)
    description = models.TextField()

    def __str__(self):
        return self.title

class Step(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()
    order = models.IntegerField(default=0)
    course = models.ForeignKey(Course)

    class Meta:
        ordering = ['order',]

    def __str__(self):
        return self.title

URLS

from django.conf.urls import url, include

from . import views

urlpatterns = [
    url(r'^$', views.course_list),
    url(r'(?P<course_pk>\d+)/(?P<step_pk>\d+)/$', views.step_detail),
    url(r'(?P<pk>\d+)/$', views.course_detail),
]
Kade Carlson
Kade Carlson
5,928 Points

If easier I can post this code to GitHub when I get to my home computer to make this easier to read

1 Answer

Brandon Harden
seal-mask
.a{fill-rule:evenodd;}techdegree
Brandon Harden
Python Web Development Techdegree Student 6,586 Points

In views.py you have to import the Step Model:

from django.shortcuts import get_object_or_404, render
from .models import Course, Step


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

def course_detail(request, pk):
    course = get_object_or_404(Course, pk=pk)
    return render(request, 'courses/course_detail.html', {'course': course})

def step_detail(request, course_pk, step_pk):
    step = get_object_or_404(Step, course_id=course_pk, pk=step_pk)
    return render(request, 'courses/step_detail.html', {'step': step})