Files
blogsite/api/views.py
T
xiaoan 6ab9cca4f3 重做文章详情页:拾光阅读版式与目录/代码/灯箱交互
- 新增内容页外壳 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 移出版本库
2026-11-20 15:37:55 +08:00

60 lines
1.9 KiB
Python

# api/views.py
from django.db import models
from rest_framework.decorators import api_view, renderer_classes
from rest_framework.response import Response
from blog.models import Category, Post, SiteInfo
from .renderers import MarkdownRenderer
@api_view(['GET'])
@renderer_classes([MarkdownRenderer])
def sitemap_markdown(request):
"""
生成站点地图的 Markdown 文本
分类按最早文章时间升序,文章按发布时间从小到大
"""
site_name = "我的博客"
try:
site_info = SiteInfo.get_site_info()
if site_info.about_title:
site_name = site_info.about_title
except Exception:
pass
lines = [f"# {site_name}", ""]
# 分类按该分类下最早发布的文章时间升序排列
# 没有文章的分类(first_post_date=None)会排在最前面
categories = Category.objects.all().order_by('name')
for cat in categories:
# 文章按 published_at 从小到大(升序)
posts = cat.posts.filter(status='published').order_by('published_at')
lines.append(f"## {cat.name}")
lines.append("")
if posts.exists():
for post in posts:
post_url = f"/post/{post.slug}/"
lines.append(f"### [{post.title}]({post_url})")
else:
lines.append("- *暂无文章*")
lines.append("")
# 未分类文章,同样按发布时间升序
uncategorized = Post.objects.filter(
categories__isnull=True,
status='published'
).order_by('published_at')
if uncategorized.exists():
lines.append("## 未分类")
lines.append("")
for post in uncategorized:
post_url = f"/post/{post.slug}/"
lines.append(f"### [{post.title}]({post_url})")
lines.append("")
markdown_text = "\n".join(lines)
return Response(markdown_text)