Django 搭建博客项目(6)-博客分类的展示
前面文章对首页博客列表展示以及相关博客详情展示、后台使用富文本编辑博客内容等功能做了简述。接下来本篇文章将继续实现首页博客分类的展示效果。
首先看下博客分类展示所在的页面位置:

可以看到分类的展示在主页的右上角部分,即在页面的 right 模块中,回顾代码看看前面的模块划分:
base.html
<!DOCTYPE html>
<!-- saved from url=(0035)http://hello123.pythonanywhere.com/ -->
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="/static/style.css">
......
{% block headerjs %}
{% endblock %}
</head>
<body>
<div id="container">
<div id="wrap">
{% include 'header.html' %}
<div class="outer">
{% block left %}
{% endblock %}
{% include 'right.html' %}
</div>
{% include 'footer.html' %}
</div>
</div>
</body>
</html>
这里 right.html 包含在前端基类 base.html 中,而且在上一篇也看到,在博客详情页面的右上角同样会有博客的分类展示效果。
既然这里是在页面的公共部分,则需要用到全局上下文对相关数据进行存储。接下来康康 setting.py 文件中系统全局上下文的定义情况。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
'django.contrib.messages.context_processors.messages' 即为系统全局上下文的组件。进入相关文件查看:
def messages(request):
"""
Return a lazy 'messages' context variable as well as
'DEFAULT_MESSAGE_LEVELS'.
"""
return {
'messages': get_messages(request),
'DEFAULT_MESSAGE_LEVELS': DEFAULT_LEVELS,
}
这里系统给提供了全局上下文数据书写的大致方法,那么我们就应该按照类似的做法来自定义全局上下文数据。
欢迎关注微信公众号【python面面观】,在聊天对话框回复「博客」获取源码地址以及其他 python 相关知识。
首先,在 post 模块下创建自定义全局上下文文件 mycontextprocessor.py 文件。同时提供获取分类信息的方法:
# coding=utf-8
from django.db.models import Count
from post.models import Post
def getCategory(request):
# 获取分类信息 根据 count的降序排序
categoryList = Post.objects.values('category_cname', 'category').annotate(c=Count('*')).order_by('-c')
return {'category_list': categoryList}
通过 getCategory 方法拿到了分类博客按照降序排列的数据。
定义完成了方法,则对应在 setting.py 文件中同样增加配置信息:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'post.mycontextprocessor.getCategory',
],
},
},
]
配置完成之后,可以在相应的 html 文件中对数据进行展示处理。进入到 right.html 文件中去处理数据。
right.html
<aside id="sidebar">
<!--分类-->
<div class="widget-wrap">
<h3 class="widget-title">分类</h3>
<div class="widget">
<ul class="category-list">
{% for categoryItem in category_list %}
<li class="category-list-item">
<a class="category-list-link" href="/category/{{ categoryItem.category }}">{{ categoryItem.category_cnmae }}</a>
<span class="category-list-count">{{ categoryItem.c }}</span>
</li>
{% endfor %}
</ul>
</div>
</div>
......
</aside>
right.html 页面对数据处理完成后,运行项目,查看显示效果:

可以看到右上角的数据已经有了变化,并按照数量进行了排序。
右上角分类展示完成后,继续将下方近期文章一并获取并展示出来。
getCategory 方法增加近期文章数据的获取:
# coding=utf-8
from django.db.models import Count
from post.models import Post
def getCategory(request):
# 获取分类信息 根据 count的降序排序
categoryList = Post.objects.values('category__cname', 'category').annotate(c=Count('*')).order_by('-c')
# 近期文章
recentBlogs = Post.objects.all().order_by('-created')[:3]
return {'category_list': categoryList, 'recent_blog': recentBlogs}
right.html
<div class="widget-wrap">
<h3 class="widget-title">近期文章</h3>
<div class="widget">
<ul>
{% for recentBlogItem in recent_blog %}
<li>
<a href="/post/{{ recentBlogItem.id }}" target="_blank">{{ recentBlogItem.title | truncatechars:12 }}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
truncatechars:12 标题最多显示12个字符,其余用省略号进行表示。运行项目,查看近期文章展示情况:

这样本篇就完成了分类以及近期文章模块相关信息的展示。