user2539745
user2539745

Reputation: 1065

IndexError: tuple index out of range

I was referring to this page of django documentation to write views. Can someone explain what I did wrong ? And what could be the solution

    self.object_list = self.get_queryset()
  File "/vagrant/projects/kodeworms/course/views.py", line 23, in get_queryset
    self.Course = get_object_or_404(Course, name=self.args[0])
  IndexError: tuple index out of range

My views.py file

# Create your views here.
from django.views.generic import ListView, DetailView
from django.shortcuts import get_object_or_404

from .models import Course, Content


class PublishedCourseMixin(object):
    def get_queryset(self):
        queryset = super(PublishedCourseMixin, self).get_queryset()
        return queryset.filter(published_course=True)


class CourseListView(PublishedCourseMixin, ListView):
    model = Course
    template_name = 'course/course_list.html'

class CourseContentListView(ListView):
    model = Content
    template_name = 'course/content_list.html'

    def get_queryset(self):
        self.Course = get_object_or_404(Course, name=self.args[0])
        return Content.objects.filter(course=self.course, published=True)

My urls.py file

from django.conf.urls import patterns, url

from . import views

urlpatterns = patterns('',
    url(r"^$", views.CourseListView.as_view(), name="list" ),
    url(r"^(?P<slug_topic_name>[\w-]+)/$", views.CourseContentListView.as_view(), name="list"),
)

Upvotes: 2

Views: 9052

Answers (2)

Burhan Khalid
Burhan Khalid

Reputation: 174624

You are using self.args[0] which is for positional arguments, but you are passing in a keyword argument to your view.

As you have no positional arguments self.args is a zero-length tuple which is why you get that exception.

You should be using self.kwargs['slug_topic_name'] since you have a keyword argument in your url.

Upvotes: 6

Sholomitcky
Sholomitcky

Reputation: 397

if you are going to this url

url(r"^$", views.CourseListView.as_view(), name="list" ),

there is no self.args, you should check it

I suppose, it will work if you will go to this url

url(r"^(?P<slug_topic_name>[\w-]+)/$", views.CourseContentListView.as_view(), name="list"),

Upvotes: 0

Related Questions