1

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"),
)
4

2 回答 2

5

您正在使用self.args[0]which is 用于位置参数,但是您正在将关键字参数传递给您的视图。

由于您没有位置参数self.args,因此是一个零长度元组,这就是您得到该异常的原因。

您应该使用self.kwargs['slug_topic_name'],因为您的 url 中有一个关键字参数。

于 2013-07-28T11:17:03.350 回答
0

如果你要去这个网址

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

没有self.args,你应该检查一下

我想,如果你去这个网址,它会起作用

url(r"^(?P<slug_topic_name>[\w-]+)/$", views.CourseContentListView.as_view(), name="list"),
于 2013-07-28T11:09:58.280 回答