我嘗試了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