You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
3.0 KiB
76 lines
3.0 KiB
# blog/views.py
|
|
from django.shortcuts import render, get_object_or_404
|
|
from .models import Post, Category
|
|
|
|
def index(request):
|
|
"""首页视图"""
|
|
post_list = Post.objects.filter(status='published').order_by('-created_time')[:10]
|
|
|
|
context = {
|
|
'post_list': post_list,
|
|
'category_info': {
|
|
'name': '非遗传承',
|
|
'label': '',
|
|
'desc': '探索南京非物质文化遗产的独特魅力与传承故事'
|
|
},
|
|
'current_path': request.path,
|
|
'current_category_id': None
|
|
}
|
|
return render(request, 'blog/index.html', context)
|
|
|
|
def category_view(request, category_id):
|
|
"""分类页面视图"""
|
|
print(f"=== 调试信息: 访问分类ID {category_id} ===")
|
|
|
|
category = get_object_or_404(Category, id=category_id)
|
|
print(f"数据库中的分类名称: '{category.name}'")
|
|
|
|
# 根据实际分类名称进行映射
|
|
if "工艺" in category.name:
|
|
label = '巧夺天工·工艺'
|
|
desc = '探索南京传统手工艺的精湛技艺与匠心传承'
|
|
print("映射到: 工艺分类")
|
|
elif "表演" in category.name or "梨园" in category.name:
|
|
label = '梨园雅韵·表演'
|
|
desc = '感受南京传统表演艺术的独特韵味与舞台魅力'
|
|
print("映射到: 表演分类")
|
|
elif "民俗" in category.name or "烟火" in category.name:
|
|
label = '人间烟火·民俗'
|
|
desc = '体验南京丰富多彩的民俗活动与民间传统'
|
|
print("映射到: 民俗分类")
|
|
elif "文学" in category.name or "口传" in category.name:
|
|
label = '口传心授·文学'
|
|
desc = '领略南京口传文学的语言艺术与文化内涵'
|
|
print("映射到: 文学分类")
|
|
elif "人物" in category.name or "匠心" in category.name:
|
|
label = '匠心传承·人物'
|
|
desc = '认识南京非物质文化遗产的传承人与守护者'
|
|
print("映射到: 人物分类")
|
|
else:
|
|
label = category.name
|
|
desc = f'探索南京{category.name}的独特魅力'
|
|
print(f"未匹配,使用默认: {category.name}")
|
|
|
|
posts = Post.objects.filter(category=category, status='published').order_by('-created_time')
|
|
|
|
context = {
|
|
'post_list': posts,
|
|
'category_info': {
|
|
'name': category.name, # 使用数据库中的名称
|
|
'label': label, # 使用映射后的显示标签
|
|
'desc': desc # 使用映射后的描述
|
|
},
|
|
'current_path': request.path,
|
|
'current_category_id': category_id
|
|
}
|
|
|
|
print(f"传递给模板的数据: name='{context['category_info']['name']}', label='{context['category_info']['label']}'")
|
|
return render(request, 'blog/index.html', context)
|
|
|
|
def detail(request, post_id):
|
|
"""文章详情页"""
|
|
post = get_object_or_404(Post, id=post_id, status='published')
|
|
post.views += 1
|
|
post.save()
|
|
context = {'post': post}
|
|
return render(request, 'blog/detail.html', context) |