Thewebframeworkforperfectionistswithdeadlines.
Note
SomeoftheexamplesonthispageassumethatanArticlemodelhasbeendefinedasfollowsinmyapp/models.py:
Ancestors(MRO)
Context
Notes
Examplemyapp/urls.py:
- {%forarticleinlatest%}
- {{article.pub_date}}:{{article.title}} {%endfor%}
Ayearlyarchivepageshowingallavailablemonthsinagivenyear.Objectswithadateinthefuturearenotdisplayedunlessyousetallow_futuretoTrue.
Abooleanspecifyingwhethertoretrievethefulllistofobjectsforthisyearandpassthosetothetemplate.IfTrue,thelistofobjectswillbemadeavailabletothecontext.IfFalse,theNonequerysetwillbeusedastheobjectlist.Bydefault,thisisFalse.
Examplemyapp/views.py:
fromdjango.views.generic.datesimportYearArchiveViewfrommyapp.modelsimportArticleclassArticleYearArchiveView(YearArchiveView):queryset=Article.objects.all()date_field="pub_date"make_object_list=Trueallow_future=TrueExamplemyapp/urls.py:
fromdjango.views.generic.datesimportMonthArchiveViewfrommyapp.modelsimportArticleclassArticleMonthArchiveView(MonthArchiveView):queryset=Article.objects.all()date_field="pub_date"allow_future=TrueExamplemyapp/urls.py:
fromdjango.views.generic.datesimportWeekArchiveViewfrommyapp.modelsimportArticleclassArticleWeekArchiveView(WeekArchiveView):queryset=Article.objects.all()date_field="pub_date"week_format="%W"allow_future=TrueExamplemyapp/urls.py:
Adayarchivepageshowingallobjectsinagivenday.Daysinthefuturethrowa404error,regardlessofwhetheranyobjectsexistforfuturedays,unlessyousetallow_futuretoTrue.
fromdjango.views.generic.datesimportDayArchiveViewfrommyapp.modelsimportArticleclassArticleDayArchiveView(DayArchiveView):queryset=Article.objects.all()date_field="pub_date"allow_future=TrueExamplemyapp/urls.py:
fromdjango.views.generic.datesimportTodayArchiveViewfrommyapp.modelsimportArticleclassArticleTodayArchiveView(TodayArchiveView):queryset=Article.objects.all()date_field="pub_date"allow_future=TrueExamplemyapp/urls.py:
Apagerepresentinganindividualobject.Iftheobjecthasadatevalueinthefuture,theviewwillthrowa404errorbydefault,unlessyousetallow_futuretoTrue.