我尝试了Django的基于类的视图,并喜欢它们到目前为止,但我无法让YearArchiveView给我任何东西。这里是我的类:Django基于类的视图YearArchiveView
class ThoughtsByYearView(YearArchiveView):
template_name = "thoughts/index_by_year.html"
queryset = Thought.objects.published()
date_field = 'pub_date'
context_object_name = 'thought_list'
和我的urls.py:
urlpatterns = patterns('thoughts.views',
url(r'^$', ThoughtsIndexView.as_view(), name='thoughts'),
url(r'^(?P<year>\d{4})/$', ThoughtsByYearView.as_view(), name='thoughts_year'),
)
两个thought_list
和object_list
回报空列表。重新定义get_queryset
也不会导致任何结果。 ThoughtsIndexView
返回正确的对象,所以我相信这只是我犯的一个愚蠢的错误。谁能告诉我它是什么?
哦,这里是失败的测试用例:(编辑:结果在浏览器中是一样的无回报)
def test_thoughts_by_year_has_thoughts(self):
response = self.client.get(reverse('thoughts_year', args=[datetime.now().year]))
thoughts_by_year = response.context_data['thought_list']
self.assertGreater(len(thoughts_by_year), 0)
这是正确的:'Thought.objects.published()'? – DTing 2011-04-20 18:37:56
是的,这是正确的。 – 2011-04-20 18:40:21