私は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)