后台处理:
views.py
def blog_time(request, year, month):
change_info(request)
postsAll = Articles.objects.all().filter(status="有效").order_by('-timestamp')
blog_list_greats = Articles.objects.filter(status="有效").order_by("-greats")[0:10]
blog_list_comments = Articles.objects.filter(status="有效").order_by("-comments")[0:10]
blog_lists = Articles.objects.filter(timestamp__year=year, timestamp__month=month, status="有效").order_by('-timestamp')
paginator = Paginator(blog_lists, 10) # 分页,每页10条数据
page = request.GET.get('page')
try:
contacts = paginator.page(page) # contacts为Page对象!
except PageNotAnInteger:
# If page is not an integer, deliver first page.
contacts = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
contacts = paginator.page(paginator.num_pages)
comments = Comment.objects.count()
tags = Tag.objects.all()
count = Articles.objects.count()
categorys = Category.objects.all()
jia = Jia.objects.get(id=1)
articles = Articles.objects.all().filter(status="有效")
year_month = set() # 设置集合,无重复元素
for a in articles:
year_month.add((a.timestamp.year, a.timestamp.month)) # 把每篇文章的年、月以元组形式添加到集合中
counter = {}.fromkeys(year_month, 0) # 以元组作为key,初始化字典
for a in articles:
counter[(a.timestamp.year, a.timestamp.month)] += 1 # 按年月统计文章数目
year_month_number = [] # 初始化列表
for key in counter:
year_month_number.append([key[0], key[1], counter[key]]) # 把字典转化为(年,月,数目)元组为元素的列表
year_month_number.sort(reverse=True) # 排序
print(year_month_number)
context = {
'tags': tags,
'contacts': contacts,
'comments': comments,
'categorys': categorys,
'blog_list_greats': blog_list_greats,
'blog_list_comments': blog_list_comments,
'blog_category': blog_category,
'jia': jia,
'year_month_number': year_month_number
}
return render(request, 'archive.html', context=context)
这一段的处理有点多余,主要是按照年月查找文章,打开相应页面
urls.py
url(r'^list/(?P<year>[0-9]{4})/(?P<month>[0-9]{1,2})/$', views.blog_time),
然后是首页展示的后台逻辑:
views.py
def blog_index(request):
# post = request.get_post(Articles, pk=pk)
change_info(request)
blog_lists = Articles.objects.filter(status="有效").order_by("-timestamp")[0:10] # 获取所有数据
blog_list_head = Articles.objects.filter(status="有效").filter(istop=1).order_by("-timestamp")[0:5] # 获取5 头部的5个
blog_list_up = Articles.objects.filter(status="有效").filter(istop=2).order_by("-timestamp")[0:4] # 获取2,上面的4个
blog_list_middle = Articles.objects.filter(status="有效").order_by('-comments')[0:2] # 获取2,中间那两个
blog_list_down = Articles.objects.filter(status="有效").order_by('-greats')[0:2] # 获取2,下面那两个
blog_list_news = Articles.objects.filter(status="有效").order_by("-timestamp")[0:10] # 获取3 recent posts
blog_list_views = Articles.objects.filter(status="有效").order_by('-views')[0:10] # 点击排行
blog_list_hots = Articles.objects.filter(status="有效").order_by('-views')[0:4] # 点击排行
blog_list_greats = Articles.objects.filter(status="有效").order_by('-greats')[0:10] # 猜你喜欢
blog_list_comments = Articles.objects.filter(status="有效").order_by('-comments')[0:10] # 博主推荐
blog_list_large = Articles.objects.filter(status="有效").order_by('-timestamp')[0:1] # 获取1,最大那个
articles = Articles.objects.all().filter(status="有效")
year_month = set() # 设置集合,无重复元素
for a in articles:
year_month.add((a.timestamp.year, a.timestamp.month)) # 把每篇文章的年、月以元组形式添加到集合中
counter = {}.fromkeys(year_month, 0) # 以元组作为key,初始化字典
for a in articles:
counter[(a.timestamp.year, a.timestamp.month)] += 1 # 按年月统计文章数目
year_month_number = [] # 初始化列表
for key in counter:
year_month_number.append([key[0], key[1], counter[key]]) # 把字典转化为(年,月,数目)元组为元素的列表
year_month_number.sort(reverse=True) # 排序
print(year_month_number)
tags = Tag.objects.all()
count = Articles.objects.count()
pagelist = round(count / 3)
pl = []
for i in range(pagelist):
pl.append(i + 1)
print((pl))
comment_list = Comment.objects.count()
note = Note.objects.get(id=str(random.randint(1, Note.objects.count())))
print(note.noteimage)
categorys = Category.objects.all()
catcharts = {}
for cats in categorys:
catnums = Articles.objects.filter(category=cats.id).filter(status='有效').count()
catcharts[cats.name] = catnums
django = Articles.objects.filter(category=1).filter(status='有效').count()
python = Articles.objects.filter(category=2).filter(status='有效').count()
mysql = Articles.objects.filter(category=3).filter(status='有效').count()
leetcode = Articles.objects.filter(category=4).filter(status='有效').count()
other = Articles.objects.filter(category=5).filter(status='有效').count()
java = Articles.objects.filter(category=6).filter(status='有效').count()
javascript = Articles.objects.filter(category=7).filter(status='有效').count()
maxmap = max(django, python, leetcode, mysql, other, java, javascript)
minmap = min(django, python, leetcode, mysql, other, java, javascript)
allvisit = VisitNumber.objects.first()
jia = Jia.objects.get(id=1)
try:
version = models.Version.objects.filter(version_time=datetime.datetime.now().strftime('%Y-%m-%d')).values(
'version_content')
versions = [item[key] for item in version for key in item][0].split(";")
except IndexError as err:
print("error", err)
version = models.Version.objects.order_by('-version_time')[0:1].values('version_content')
versions = [item[key] for item in version for key in item][0].split(";")
'''else:
version = models.Version.objects.all()[0:1].values('version_content')
versions = [item[key] for item in version for key in item][0].split(";")'''
context = {
'blog_list': blog_lists,
'blog_list_views': blog_list_views,
'blog_list_greats': blog_list_greats,
'blog_list_comments': blog_list_comments,
'tags': tags,
'pagelists': pl,
'comment_list': comment_list,
'note': note,
'categorys': categorys,
'count': count,
'blog_list_five': blog_list_head,
'blog_list_up': blog_list_up,
'blog_list_middel': blog_list_middle,
'blog_list_down': blog_list_down,
'blog_list_three': blog_list_news,
'blog_list_hots': blog_list_hots,
'blog_list_large': blog_list_large,
'django': django,
'python': python,
'mysql': mysql,
'leetcode': leetcode,
'other': other,
'java': java,
'javascript': javascript,
'max': maxmap,
'min': minmap,
'versions': versions,
'allvisit': allvisit,
'catcharts': catcharts,
'jia': jia,
'year_month_number':year_month_number,
}
return render(request, 'Jiaindex.html', context=context) # 返回Jiaindex.html页面
主要部分是:
articles = Articles.objects.all().filter(status="有效")
year_month = set() # 设置集合,无重复元素
for a in articles:
year_month.add((a.timestamp.year, a.timestamp.month)) # 把每篇文章的年、月以元组形式添加到集合中
counter = {}.fromkeys(year_month, 0) # 以元组作为key,初始化字典
for a in articles:
counter[(a.timestamp.year, a.timestamp.month)] += 1 # 按年月统计文章数目
year_month_number = [] # 初始化列表
for key in counter:
year_month_number.append([key[0], key[1], counter[key]]) # 把字典转化为(年,月,数目)元组为元素的列表
year_month_number.sort(reverse=True) # 排序
这时候传入前端的是一个列表:
[[2019, 7, 5], [2019, 6, 8], [2019, 5, 2], [2019, 4, 3], [2019, 3, 11], [2019, 2, 34]]
然后前端的html部分:
<div class="widget widget_categories version-ii wow fadeInUp" data-wow-delay="0.4s">
<header class="widget-head">
<h3>Time</h3>
</header>
<ul>
{% for i in year_month_number %}
<li class="cat-item cat-item-1"><!--span><a href="#">- Lifestyle</a></span--><span><a href="/JiaBlog/list/{{ i.0 }}/{{ i.1 }}" target="_blank">- {{ i.0 }}年{{ i.1 }}月---({{ i.2 }})</a></span></li>
{% endfor %}
</ul>
</div>