django 按年月日归档

2023-02-17 15:04:34 浏览数 (1)

view.py

代码语言:javascript复制
class TimlelineView(generic.ListView):
    model = Article
    template_name = 'timeline.html'
    context_object_name = "article"

    def get_queryset(self):
        author_name = self.kwargs['author_name']
        article_list = Article.objects.filter(show_status=True).order_by("-time_created")
        return article_list

or

代码语言:javascript复制
def timeline(request):
    article_list = Article.objects.filter(show_status=True).order_by("-time_created")
    return render(request, "timeline.html", locals())

timeline.html

代码语言:javascript复制
   <div class="am-g am-g-fixed blog-fixed blog-content">
        <div class="am-u-sm-12">
            <h1 class="blog-text-center">-- 存档 --</h1>
            {#            {{ article_list }}#}
            {% regroup article_list by time_created.year as year_post_group %}
            {% for year in year_post_group %}
                <div class="timeline-year">
                    <h1>{{ year.grouper }}年</h1>
                    <hr>
                    {% regroup year.list by time_created.month as month_post_group %}
                    <ul>
                        {% for month in month_post_group %}
                            <h2>{{ month.grouper }}月</h2>
                            <hr>
                            {% regroup month.list by time_created.day as day_post_group %}
                            {% for day in day_post_group %}
                                <div class="timeline-day">
                                    {{ day.grouper }}日
                                    <ul>
                                        {% for article in day.list %}
                                            <li>
                                                <a href="{% url "blog:detail" article.id %}">{{ article.title }}</a>
                                            </li>
                                        {% endfor %}
                                    </ul>
                                </div>
                            {% endfor %}
                        {% endfor %}
                    </ul>
                </div>
            {% endfor %}
        </div>
    </div>

0 人点赞