我指的是django documentation的這個頁面來編寫視圖。有人能解釋我做錯了什麼嗎?有什麼能解決IndexError:元組索引超出範圍
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
我views.py文件
# 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)
我的urls.py文件
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"),
)
你可以發佈你的整個def get_queryset(self)嗎?同樣,因爲我懷疑你已經遵循了教程,你如何訪問這個視圖? –
我猜'self.args'是空的。 – iMom0
@limelights更新了我的問題 – user2539745