Django Blog Archive-- Display List of Years and Months that Include Post

时间:2018-03-23 00:39:57

标签: python django

I'm trying to do something that I thought would be relatively simple, considering Django was built for news sites. Just want to get an archive list grouped by year/month count on the sidebar of my blog. Something like

2018
 -Mar(3)
 -Feb (2)
 -Jan (6)

I've done a lot of searching on here, but seems like it's been a while since anyone was looking to do something like this and everything else is outdated.

I have been able to figure out how to create a monthly archive url/view/tempate and can display a month's posts by going to blog/2018/03 here is my blog's views

class BlogListView(generic.ListView):
    model = Post
    context_object_name = 'post_list'
    queryset = Post.objects.all()
    template_name = 'post_list.html'

class ArticleMonthArchiveView(MonthArchiveView):
    queryset = Post.objects.all()
    date_field = "posted"
    allow_future = True

def blog_post(request, slug):
    blog_post = Post.objects.get(slug=slug)
    context = {'blog_post': blog_post}
    return render(request, 'blog/blog_post.html', context)

and my urls (please ignore mix of url and path for now--working on that!)

urlpatterns = [
    url(r'^$', BlogListView.as_view(), name='blog'),
    url(r'^(?P<slug>[\w-]+)/$', blog_post, name='blog_post'),
    path('<int:year>/<int:month>/',
         ArticleMonthArchiveView.as_view(month_format='%m'),
         name="post_archive_month"),
]

I think i will need to create another context field on all of my views in order to do this? But i don't really know where to start. I know that I could obviously manually create a list of months and loop to try and see if posts match but I can't imagine that is the most efficient way.

Anyone have a fairly obvious solution that i might be missing here?

1 个答案:

答案 0 :(得分:4)

我就是这样做的。它可能看起来太复杂,无法实现您的目标。但这给了很多控制权。

您想要添加&#39; context_processor&#39;在您的博客应用中,可以在所有页面上显示帖子列表。

项目/博客/ context_processors.py

from .models import Post

def posts(request):
    return {
        'all_posts': Post.objects.order_by('posted'),
    }

在TEMPLATES中的settings.py上添加它 - &gt;选项 - &gt; context_processors

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            # ...
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'debug':True,
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                # ....
                'django.contrib.messages.context_processors.messages',
                'project.blog.context_processors.posts', ### ADDED LINE
            ],
        },
    },
]

将其添加到基本模板的侧边栏中 base.html文件

<div id='site__side'>
    ...
    {% regroup all_posts by posted.year as year_list %}

    <ul>
    {% for year in year_list %}
        <li>{{ year.grouper }}
            {% regroup year.list by posted.month as month_list %}
            <ul>
            {% for month in month_list %}
                <li><a href="{% url 'blog:post_archive_month' year.grouper month.grouper %}">{{ month.list.0.posted|date:'b' }} ({{ month.list|length }})</a></li>
            {% endfor %}
            </ul>
        </li>
    {% endfor %}
    </ul>
    ...
</div>

{{month.list.0.posted | date:&#39; b&#39;是否以3个字母的文本格式显示月份

编辑:{{month.list | length}}以计算