update. 完成分类显示与详情页显示效果
This commit is contained in:
+2
-1
@@ -12,9 +12,10 @@ urlpatterns = [
|
||||
path('about/', views.about, name='about'),
|
||||
path('news/', views.news, name='news'),
|
||||
path('info/', views.info, name='info'),
|
||||
path('service/', views.service, name='service'),
|
||||
path('news/<str:sub>.html', views.sub_news_list, name='news_list'),
|
||||
path('party/', views.party, name='party'),
|
||||
path('interaction/', views.interaction, name='interaction'),
|
||||
path('science/', views.science, name='science'),
|
||||
path('article/<int:pk>.html/', views.article_detail, name='article_detail'),
|
||||
path('news/<int:pk>.html/', views.news_detail, name='news_detail'),
|
||||
]
|
||||
|
||||
+59
-80
@@ -9,7 +9,7 @@ from loguru import logger
|
||||
|
||||
from article.models import Article
|
||||
from base.models import MainMenu
|
||||
from news.models import NewsSection
|
||||
from news.models import NewsSection, News
|
||||
from utils.format import get_now_strftime_format
|
||||
|
||||
|
||||
@@ -32,91 +32,35 @@ def article_detail(request, pk: int):
|
||||
logger.error(traceback.format_exc())
|
||||
return render(request, 'front/error/500.html')
|
||||
|
||||
def news_detail(request, pk: int):
|
||||
"""
|
||||
根据主键 (pk) 获取文章详情
|
||||
"""
|
||||
try:
|
||||
# 使用 get_object_or_404 简化代码:如果文章不存在,自动返回 404 页面
|
||||
article = get_object_or_404(News, pk=pk)
|
||||
# 如果是渲染 HTML 模板
|
||||
return render(request, 'front/details/news.html', {'article': article})
|
||||
except (News.DoesNotExist, Http404):
|
||||
return render(request, 'front/error/404.html')
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
logger.error(traceback.format_exc())
|
||||
return render(request, 'front/error/500.html')
|
||||
|
||||
|
||||
def index(request):
|
||||
# 时间字符串
|
||||
time_str = get_now_strftime_format()
|
||||
# 新闻内容
|
||||
news_sections = NewsSection.objects.filter(visible=True).order_by('order')
|
||||
news_data = {
|
||||
"key": "important",
|
||||
"key": "news",
|
||||
"data": {
|
||||
'消防要闻': [
|
||||
{'title': '习近平会见全国应急管理系统先进模范和消防忠诚卫士表彰...', 'date': '2021-11-06'},
|
||||
{'title': '北京市消防救援局挂牌', 'date': '2024-06-19'},
|
||||
{'title': '市消防救援局即日起开展专项行动,推出一揽子“便民利企”措施', 'date': '2025-07-01'},
|
||||
{'title': '北京开展石油化工生产装置灭火救援实战演练', 'date': '2025-09-30'},
|
||||
{'title': '北京市全面启动 2025 年度消防宣传月活动', 'date': '2025-11-11'},
|
||||
{'title': '逛庙会学消防!厂甸大观园庙会打造消防“打卡点”', 'date': '2026-02-25'},
|
||||
{'title': '“火焰蓝”守护年味!消防员坚守景区庙会护平安', 'date': '2026-02-21'},
|
||||
],
|
||||
'基层动态': [
|
||||
{'title': '某支队开展冬季练兵比武活动', 'date': '2023-12-01'},
|
||||
{'title': '社区微型消防站进行应急演练', 'date': '2023-12-05'},
|
||||
{'title': '朝阳区举办消防安全知识讲座', 'date': '2024-01-15'},
|
||||
],
|
||||
'国务院新闻': [
|
||||
{'title': '国务院安委会部署全国安全生产大检查', 'date': '2023-11-20'},
|
||||
]
|
||||
section.title: section.news_set.filter(is_published=True).order_by("-created_at")
|
||||
for section in news_sections
|
||||
}
|
||||
}
|
||||
# section_data = [
|
||||
# {
|
||||
# "key": "info",
|
||||
# "data": {
|
||||
# '信息公开': [
|
||||
# {'title': '习近平会见全国应急管理系统先进模范和消防忠诚卫士表彰...', 'date': '2021-11-06'},
|
||||
# {'title': '北京市消防救援局挂牌', 'date': '2024-06-19'},
|
||||
# {'title': '市消防救援局即日起开展专项行动,推出一揽子“便民利企”措施', 'date': '2025-07-01'},
|
||||
# {'title': '北京开展石油化工生产装置灭火救援实战演练', 'date': '2025-09-30'},
|
||||
# {'title': '北京市全面启动 2025 年度消防宣传月活动', 'date': '2025-11-11'},
|
||||
# {'title': '逛庙会学消防!厂甸大观园庙会打造消防“打卡点”', 'date': '2026-02-25'},
|
||||
# {'title': '“火焰蓝”守护年味!消防员坚守景区庙会护平安', 'date': '2026-02-21'},
|
||||
# ],
|
||||
# },
|
||||
# },
|
||||
# {
|
||||
# "key": "party",
|
||||
# "data": {
|
||||
# '党建引领': [
|
||||
# {'title': '习近平会见全国应急管理系统先进模范和消防忠诚卫士表彰...', 'date': '2021-11-06'},
|
||||
# {'title': '北京市消防救援局挂牌', 'date': '2024-06-19'},
|
||||
# {'title': '市消防救援局即日起开展专项行动,推出一揽子“便民利企”措施', 'date': '2025-07-01'},
|
||||
# {'title': '北京开展石油化工生产装置灭火救援实战演练', 'date': '2025-09-30'},
|
||||
# {'title': '北京市全面启动 2025 年度消防宣传月活动', 'date': '2025-11-11'},
|
||||
# {'title': '逛庙会学消防!厂甸大观园庙会打造消防“打卡点”', 'date': '2026-02-25'},
|
||||
# {'title': '“火焰蓝”守护年味!消防员坚守景区庙会护平安', 'date': '2026-02-21'},
|
||||
# ],
|
||||
# },
|
||||
# },
|
||||
# {
|
||||
# "key": "interaction",
|
||||
# "data": {
|
||||
# '互动交流': [
|
||||
# {'title': '习近平会见全国应急管理系统先进模范和消防忠诚卫士表彰...', 'date': '2021-11-06'},
|
||||
# {'title': '北京市消防救援局挂牌', 'date': '2024-06-19'},
|
||||
# {'title': '市消防救援局即日起开展专项行动,推出一揽子“便民利企”措施', 'date': '2025-07-01'},
|
||||
# {'title': '北京开展石油化工生产装置灭火救援实战演练', 'date': '2025-09-30'},
|
||||
# {'title': '北京市全面启动 2025 年度消防宣传月活动', 'date': '2025-11-11'},
|
||||
# {'title': '逛庙会学消防!厂甸大观园庙会打造消防“打卡点”', 'date': '2026-02-25'},
|
||||
# {'title': '“火焰蓝”守护年味!消防员坚守景区庙会护平安', 'date': '2026-02-21'},
|
||||
# ],
|
||||
# },
|
||||
# },
|
||||
# {
|
||||
# "key": "science",
|
||||
# "data": {
|
||||
# '消防科普': [
|
||||
# {'title': '习近平会见全国应急管理系统先进模范和消防忠诚卫士表彰...', 'date': '2021-11-06'},
|
||||
# {'title': '北京市消防救援局挂牌', 'date': '2024-06-19'},
|
||||
# {'title': '市消防救援局即日起开展专项行动,推出一揽子“便民利企”措施', 'date': '2025-07-01'},
|
||||
# {'title': '北京开展石油化工生产装置灭火救援实战演练', 'date': '2025-09-30'},
|
||||
# {'title': '北京市全面启动 2025 年度消防宣传月活动', 'date': '2025-11-11'},
|
||||
# {'title': '逛庙会学消防!厂甸大观园庙会打造消防“打卡点”', 'date': '2026-02-25'},
|
||||
# {'title': '“火焰蓝”守护年味!消防员坚守景区庙会护平安', 'date': '2026-02-21'},
|
||||
# ],
|
||||
# },
|
||||
# }
|
||||
# ]
|
||||
|
||||
articles_qs = Article.objects.filter(is_published=True).order_by('-created_at')
|
||||
main_sections = MainMenu.objects.filter(parent__isnull=True, visible=True).order_by('order').prefetch_related(
|
||||
# 预取可见的子板块
|
||||
@@ -184,12 +128,12 @@ def about(request):
|
||||
|
||||
|
||||
def news(request):
|
||||
sections = NewsSection.objects.filter(enabled=True).all().order_by('order')
|
||||
sections = NewsSection.objects.filter(visible=True).all().order_by('order')
|
||||
section_data = [
|
||||
{
|
||||
"key": section.code,
|
||||
"data": {
|
||||
section.name: section.news_set.filter(is_published=True)[:7],
|
||||
section.title: section.news_set.filter(is_published=True)[:7],
|
||||
}
|
||||
}
|
||||
for section in sections
|
||||
@@ -260,6 +204,41 @@ def main_article_list(request, main):
|
||||
return render(request, 'front/list.html', context=context)
|
||||
|
||||
|
||||
def sub_news_list(request, sub: str):
|
||||
logger.info(f"正在访问 {sub}")
|
||||
sections = NewsSection.objects.all().order_by('order')
|
||||
logger.info(f"新闻板块: {sections}")
|
||||
main_section = {
|
||||
'title': '新闻中心',
|
||||
'code': 'news'
|
||||
}
|
||||
sub_section = sections.filter(code=sub).first()
|
||||
logger.info(f"主板块 {main_section} 子板块 {sub_section}")
|
||||
if not sub_section or not main_section:
|
||||
raise Http404("不存在的板块")
|
||||
context = {
|
||||
'main_section': main_section,
|
||||
'sub_section': sub_section,
|
||||
'sections': sections,
|
||||
'articles': sub_section.news_set.filter(is_published=True).order_by('-created_at')[:21]
|
||||
}
|
||||
return render(request, 'front/list.html', context=context)
|
||||
|
||||
|
||||
def main_news_list(request):
|
||||
logger.info("正在访问新闻中心")
|
||||
news_list = News.objects.all()
|
||||
news_sections = NewsSection.objects.all()
|
||||
return render(request, 'front/list.html', {
|
||||
'main_section': {
|
||||
'title': '新闻中心',
|
||||
'code': 'news'
|
||||
},
|
||||
'articles': news_list,
|
||||
'sections': news_sections,
|
||||
})
|
||||
|
||||
|
||||
def sub_article_list(request, main: str, sub: str):
|
||||
"""信息公开"""
|
||||
logger.info(f"正在访问 {main}")
|
||||
|
||||
Reference in New Issue
Block a user