Django做的好出色啊,让人觉得好简单,很多地方都给你写好了,你只需要调用就可以了
比方说刚刚想写博客的归档,就想怎么完成,结果发现通用视图中就有这个,你只需要设置一下就可以了
仅仅几行代码,简直666
比方说月度的归档:
url如下:
url(r'^(?P<year>[0-9]{4})/(?P<month>[0-9]+)/$', ArticleMonthArchiveView.as_view(month_format='%m'), name="archive_month_numeric"),
视图类如下:
class ArticleMonthArchiveView(MonthArchiveView): queryset = Article.objects.all() date_field = 'create_at' allow_future = True template_name = 'article_archive_month.html'
模板就用官网的试试就行了:
<ul> {% for article in object_list %} <li>{{ article.create_at|date:"F j, Y" }}: {{ article.title }}</li> {% endfor %} </ul> <p> {% if previous_month %} Previous Month: {{ previous_month|date:"F Y" }} {% endif %} {% if next_month %} Next Month: {{ next_month|date:"F Y" }} {% endif %} </p>
是不是相当简单呢
参考:
https://docs.djangoproject.com/en/1.8/ref/class-based-views/generic-date-based/