我正在尝试 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)