Django+Bootstrap+Mysql 搭建个人博客(四)
4.1.博客分类
(1)blog_tags.py
@register.simple_tag def get_categories(): return Category.objects.all() @register.simple_tag def get_entry_count_of_category(category_name): return Entry.objects.filter(category__name=category_name).count()
(2)right_side_bar.html
<div class="row"> <div class="widget"> <h3>分类:</h3> {% get_categories as category_list %} <ul class="list-group"> {% for category in category_list %} <li class="list-group-item"> <a href="{% url \'blog:blog_category\' category.id %}">{{ category.name }}</a> <span class="badge">{% get_entry_count_of_category category.name %}</span> </li> {% endfor %} </ul> </div> </div>
4.2.博客归档
(1)urls.py
url(r\'^archives/(?P<year>[0-9]+)/(?P<month>[0-9]+)$\', views.archives, name=\'blog_archives\'),
(2)views.py
def archives(request, year, month): entries = models.Entry.objects.filter(created_time__year=year, created_time__month=month) page = request.GET.get(\'page\', 1) entry_list, paginator = make_paginator(entries, page) page_data = pagination_data(paginator, page) return render(request, \'blog/index.html\', locals())
(3)blog_tags.py
@register.simple_tag def archives(): return Entry.objects.dates(\'created_time\', \'month\', order=\'DESC\') @register.simple_tag def get_entry_count_of_date(year, month): return Entry.objects.filter(created_time__year=year, created_time__month=month).count()
(4)right_side_bar.html
<div class="row"> <div class="widget"> <h3>归档:</h3> {% archives as date_list %} <ul class="list-group"> {% for date in date_list %} <li class="list-group-item"> <a href="{% url \'blog:blog_archives\' date.year date.month %}"> <i class="glyphicon glyphicon-chevron-right"></i> {{ date.year }} 年 {{ date.month }} 月 <span class="badge">{% get_entry_count_of_date date.year date.month %}</span> </a> </li> {% endfor %} </ul> </div> </div>
4.3.标签云
(1)blog_tags.py
@register.simple_tag def get_tags(): return Tag.objects.all()
(2)right_side_bar.html
<div class="row"> <div class="widget" > <h3>标签云:</h3> {% get_tags as tag_list %} {% for tag in tag_list %} <a href="{% url \'blog:blog_tag\' tag.id %}" style="font-size: 20px;"> <span style="padding: 5px;" class="label {% cycle \'label-default\' \'label-primary\' \'label-success\' \'label-info\' \'label-warning\' \'label-danger\' %}">{{ tag.name }}</span> </a> {% endfor %} </div> </div>
4.4.RSS订阅
(1)blog/feed.py
from django.contrib.syndication.views import Feed # from django.urls import reverse from .models import Entry class LatestEntriesFeed(Feed): title = "Zhang_derek的博客" link = "/siteblogs/" description = "zhang_derek的最新博客文章!" def items(self): return Entry.objects.order_by(\'-created_time\')[:5] def item_title(self, item): return item.title def item_description(self, item): return item.abstract
(2)website/urls.py
from blog.feed import LatestEntriesFeed url(r\'^latest/feed/$\', LatestEntriesFeed()), #RSS订阅
(3)right_side_bar.html
<div class="row"> <div class="rss"> <a href="/latest/feed/"><i class="glyphicon glyphicon-plus"></i>RSS 订阅</a> </div> </div>