重做文章详情页:拾光阅读版式与目录/代码/灯箱交互
- 新增内容页外壳 base_content.html 与 reading.css / reading.js - 重写 post_detail.html:分类胶囊、阅读进度、首字下沉、代码块复制、 侧栏目录、上下篇、图片灯箱、明暗主题 - post_detail 视图补充上下篇、字数与阅读时长 - base.html 补 extra_css / extra_js block,修复脑图详情页样式不加载 - 加入 mindmaps、api、首页静态资源与模板 - 忽略 __pycache__ / *.pyc / *.back,并将已跟踪的 .pyc 移出版本库
This commit is contained in:
+74
-11
@@ -1,21 +1,53 @@
|
||||
import math
|
||||
import re
|
||||
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.core.paginator import Paginator
|
||||
from django.utils.html import strip_tags
|
||||
from .models import Category, Post, Tag , SiteInfo
|
||||
|
||||
from mindmaps.models import MindMap
|
||||
|
||||
def category_list(request):
|
||||
"""首页:展示所有分类"""
|
||||
category_list = Category.objects.all()
|
||||
# """首页:展示所有分类"""
|
||||
# category_list = Category.objects.all()
|
||||
|
||||
paginator = Paginator(category_list, 6) # 每页 10 个分类
|
||||
page_number = request.GET.get('page')
|
||||
categories = paginator.get_page(page_number)
|
||||
# paginator = Paginator(category_list, 6) # 每页 10 个分类
|
||||
# page_number = request.GET.get('page')
|
||||
# categories = paginator.get_page(page_number)
|
||||
### 第二版new
|
||||
# ===== 新增:文章数量 & 分类数量 =====
|
||||
total_posts = Post.objects.filter(status='published').count() # 已发布文章总数
|
||||
total_categories = Category.objects.count() # 分类总数
|
||||
mindmap_count = MindMap.objects.count() # 脑图数量
|
||||
# ===== 新增:最新文章(取前5篇)=====
|
||||
# latest_posts = Post.objects.filter(
|
||||
# status='published'
|
||||
# ).prefetch_related('categories', 'tags')[:5]
|
||||
|
||||
return render(request, 'category_list.html', {
|
||||
'categories': categories,
|
||||
# ===== 新增:站点信息 =====
|
||||
#site_info = SiteInfo.get_site_info()
|
||||
|
||||
# ===== 新增:每个分类各有多少篇文章(用于侧边栏/首页展示)=====
|
||||
# categories_with_count = Category.objects.annotate(
|
||||
# post_count=Count(
|
||||
# 'posts',
|
||||
# filter=models.Q(posts__status='published')
|
||||
# )
|
||||
# ).filter(post_count__gt=0).order_by('-post_count')
|
||||
|
||||
|
||||
|
||||
return render(request, 'base2.html', {
|
||||
# 'categories': categories,
|
||||
'total_posts': total_posts, # 文章总数
|
||||
'total_categories': total_categories, # 分类总数
|
||||
'mindmap_count' : mindmap_count
|
||||
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
def category_detail(request, slug):
|
||||
"""分类详情:该分类下的文章列表"""
|
||||
category = get_object_or_404(Category, slug=slug)
|
||||
@@ -32,9 +64,39 @@ def category_detail(request, slug):
|
||||
|
||||
|
||||
def post_detail(request, slug):
|
||||
"""文章详情"""
|
||||
post = get_object_or_404(Post, slug=slug, status='published')
|
||||
return render(request, 'post_detail.html', {'post': post})
|
||||
"""文章详情:正文 + 上下篇 + 字数/阅读时长"""
|
||||
post = get_object_or_404(
|
||||
Post.objects.prefetch_related('categories', 'tags'),
|
||||
slug=slug,
|
||||
status='published',
|
||||
)
|
||||
|
||||
# 同一时间轴上的已发布文章,按(发布时间, id)排序后取相邻两篇
|
||||
published_ids = list(
|
||||
Post.objects.filter(status='published')
|
||||
.order_by('published_at', 'id')
|
||||
.values_list('id', flat=True)
|
||||
)
|
||||
prev_post = next_post = None
|
||||
if post.id in published_ids:
|
||||
index = published_ids.index(post.id)
|
||||
if index > 0:
|
||||
prev_post = Post.objects.filter(id=published_ids[index - 1]).first()
|
||||
if index < len(published_ids) - 1:
|
||||
next_post = Post.objects.filter(id=published_ids[index + 1]).first()
|
||||
|
||||
# 阅读统计:先去掉 HTML 标签,再按非空白字符计数
|
||||
plain_text = strip_tags(post.content or '')
|
||||
char_count = len(re.sub(r'\s+', '', plain_text))
|
||||
read_minutes = max(1, math.ceil(char_count / 400))
|
||||
|
||||
return render(request, 'post_detail.html', {
|
||||
'post': post,
|
||||
'prev_post': prev_post,
|
||||
'next_post': next_post,
|
||||
'char_count': char_count,
|
||||
'read_minutes': read_minutes,
|
||||
})
|
||||
|
||||
|
||||
def tag_detail(request, slug):
|
||||
@@ -49,6 +111,7 @@ def tag_detail(request, slug):
|
||||
return render(request, 'tag_detail.html', {
|
||||
'tag': tag,
|
||||
'posts': posts,
|
||||
"container_wide": True,
|
||||
})
|
||||
|
||||
def about_page(request):
|
||||
|
||||
Reference in New Issue
Block a user