From 66dd4916d1e89e78be1e44f362b4df32bafeeea2 Mon Sep 17 00:00:00 2001 From: ngfchl Date: Thu, 2 Apr 2026 00:49:21 +0800 Subject: [PATCH] =?UTF-8?q?update.=20=E5=AE=8C=E5=96=84=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E9=85=8D=E7=BD=AE=E4=BB=A5=E5=8F=8A=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E9=93=BE=E6=8E=A5=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/admin.py | 6 +- base/context_processors.py | 16 ++ .../0003_baseinfo_delete_footerlink.py | 39 ++++ base/models.py | 11 +- base/templatetags/__init__.py | 0 base/templatetags/footer_tags.py | 38 ---- front/urls.py | 15 +- front/views.py | 17 +- server/settings.py | 1 + templates/base.html | 203 +++++++++--------- templates/components/footer.html | 95 -------- templates/components/main_menu.html | 10 - templates/front/details/news.html | 6 +- templates/front/index.html | 2 +- templates/front/info.html | 2 +- templates/front/list.html | 30 ++- templates/front/news.html | 2 +- 17 files changed, 218 insertions(+), 275 deletions(-) create mode 100644 base/context_processors.py create mode 100644 base/migrations/0003_baseinfo_delete_footerlink.py delete mode 100644 base/templatetags/__init__.py delete mode 100644 base/templatetags/footer_tags.py delete mode 100644 templates/components/footer.html delete mode 100644 templates/components/main_menu.html diff --git a/base/admin.py b/base/admin.py index 30023fd..c4171e8 100644 --- a/base/admin.py +++ b/base/admin.py @@ -1,7 +1,7 @@ from django.contrib import admin from import_export.admin import ImportExportModelAdmin -from base.models import MainMenu, FooterLink, LinkCategory, FriendLink +from base.models import MainMenu, BaseInfo, LinkCategory, FriendLink # —————— 主菜单 —————— @@ -61,7 +61,7 @@ class FriendLinkAdmin(ImportExportModelAdmin): # —————— 页脚链接 —————— -@admin.register(FooterLink) -class FooterLinkAdmin(ImportExportModelAdmin): +@admin.register(BaseInfo) +class BaseInfoAdmin(ImportExportModelAdmin): list_display = ('title', 'address', 'unit', 'postal', 'telephone', 'fax', 'filing') list_editable = ('postal', 'telephone', 'fax', 'unit', 'filing',) diff --git a/base/context_processors.py b/base/context_processors.py new file mode 100644 index 0000000..7ed907d --- /dev/null +++ b/base/context_processors.py @@ -0,0 +1,16 @@ +from base.models import BaseInfo, LinkCategory, MainMenu + + +def site_info(request): + """ + 为所有模板提供站点信息 + """ + site_info_obj = BaseInfo.objects.filter(is_active=True).first() + link_categories = LinkCategory.objects.filter(is_active=True).prefetch_related('links') + menus = MainMenu.objects.filter(visible=True, parent__isnull=True).order_by('order') + + return { + 'site_info': site_info_obj, + 'link_categories': link_categories, + 'menus': menus, + } diff --git a/base/migrations/0003_baseinfo_delete_footerlink.py b/base/migrations/0003_baseinfo_delete_footerlink.py new file mode 100644 index 0000000..5351258 --- /dev/null +++ b/base/migrations/0003_baseinfo_delete_footerlink.py @@ -0,0 +1,39 @@ +# Generated by Django 6.0.3 on 2026-04-01 14:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('base', '0002_alter_friendlink_options_alter_linkcategory_options_and_more'), + ] + + operations = [ + migrations.CreateModel( + name='BaseInfo', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')), + ('updated_at', models.DateTimeField(auto_now=True, verbose_name='更新时间')), + ('title', models.CharField(max_length=128, verbose_name='单位名称')), + ('e_title', models.CharField(max_length=256, verbose_name='英文名称')), + ('address', models.CharField(max_length=200, verbose_name='单位地址')), + ('postal', models.CharField(default='', verbose_name='邮编')), + ('telephone', models.CharField(default='', verbose_name='服务热线')), + ('fax', models.CharField(default='', verbose_name='传真')), + ('unit', models.CharField(default='', verbose_name='版权单位')), + ('filing', models.CharField(default='', verbose_name='备案号')), + ('official_account', models.ImageField(default='', upload_to='carousel/', verbose_name='公众号二维码')), + ('weibo', models.ImageField(default='', upload_to='carousel/', verbose_name='微博二维码')), + ('is_active', models.BooleanField(default=False, verbose_name='生效')), + ], + options={ + 'verbose_name': '单位信息', + 'verbose_name_plural': '单位信息', + }, + ), + migrations.DeleteModel( + name='FooterLink', + ), + ] diff --git a/base/models.py b/base/models.py index 1ae3b32..fbc2299 100644 --- a/base/models.py +++ b/base/models.py @@ -114,8 +114,9 @@ class MainMenu(BaseEntity): return self.children.filter(visible=True) -class FooterLink(BaseEntity): - title = models.CharField("页脚信息", max_length=50) +class BaseInfo(BaseEntity): + title = models.CharField("单位名称", max_length=128) + e_title = models.CharField("英文名称", max_length=256) address = models.CharField("单位地址", max_length=200) postal = models.CharField("邮编", default="") telephone = models.CharField("服务热线", default="") @@ -127,8 +128,8 @@ class FooterLink(BaseEntity): is_active = models.BooleanField("生效", default=False) class Meta: - verbose_name = "页脚信息" - verbose_name_plural = "页脚管理" + verbose_name = "单位信息" + verbose_name_plural = "单位信息" def __str__(self): return f"{self.title} - 状态:{'使用中' if self.is_active else '已停用'}" @@ -138,7 +139,7 @@ class FooterLink(BaseEntity): if self.is_active: # 2. 将数据库中【除了当前这条】以外的所有 FooterLink 都更新为“不生效” # self.pk 是当前对象的唯一标识 - FooterLink.objects.exclude(pk=self.pk).update(is_active=False) + BaseInfo.objects.exclude(pk=self.pk).update(is_active=False) # 3. 执行正常的保存操作 super().save(*args, **kwargs) diff --git a/base/templatetags/__init__.py b/base/templatetags/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/base/templatetags/footer_tags.py b/base/templatetags/footer_tags.py deleted file mode 100644 index 31eea3c..0000000 --- a/base/templatetags/footer_tags.py +++ /dev/null @@ -1,38 +0,0 @@ -from django import template -from loguru import logger - -from ..models import FooterLink, LinkCategory, MainMenu # 假设你的模型在当前目录的上一级 models.py 中 - -register = template.Library() - - -# inclusion_tag 装饰器会自动帮你渲染指定的模板 -@register.inclusion_tag('components/footer.html') -def show_footer(): - # 1. 获取当前生效的那一条页脚数据 - # 根据你之前的模型,is_active=True 的只有一条 - footer = FooterLink.objects.filter(is_active=True).first() - link_categories = LinkCategory.objects.filter(is_active=True) - logger.info(footer) - logger.info(link_categories) - - # 3. 将数据传递给模板 - return { - 'footer': footer, - 'categories': link_categories - } - - -@register.inclusion_tag('components/main_menu.html', takes_context=True) -def show_main_menu(context): - request = context['request'] - # 1. 获取当前生效的那一条页脚数据 - # 根据你之前的模型,is_active=True 的只有一条 - menus = MainMenu.objects.filter(visible=True, parent__isnull=True).order_by('order') - logger.info(menus) - - # 3. 将数据传递给模板 - return { - 'menus': menus, - 'request': request, - } diff --git a/front/urls.py b/front/urls.py index 616c709..b431544 100644 --- a/front/urls.py +++ b/front/urls.py @@ -1,4 +1,4 @@ -from django.urls import path, include +from django.urls import path, include, re_path from front import views @@ -7,11 +7,12 @@ app_name = 'front' # 可选,用于命名空间(如 {% url 'main:index' %} urlpatterns = [ path('', views.index, name='index'), path('.html', views.section, name='section'), - path('/list.html', views.main_article_list, name='main_list'), - path('/list/.html', views.sub_article_list, name='sub_list'), - path('about/', views.about, name='about'), - path('news/', views.news, name='news'), + path('list/news.html', views.main_news_list, name='news_main'), + path('list/.html', views.main_article_list, name='main_list'), + path('list//.html', views.sub_article_list, name='sub_list'), + path('news/.html', views.news_detail, name='news_detail'), + path('about', views.about, name='about'), path('news/.html', views.sub_news_list, name='news_list'), - path('article/.html/', views.article_detail, name='article_detail'), - path('news/.html/', views.news_detail, name='news_detail'), + re_path('news$', views.news, name='news'), + path('article/.html', views.article_detail, name='article_detail'), ] diff --git a/front/views.py b/front/views.py index ac7e57b..93e96dd 100644 --- a/front/views.py +++ b/front/views.py @@ -121,12 +121,12 @@ def news(request): sections = NewsSection.objects.filter(visible=True).all().order_by('order') section_data = [ { - "key": section.code, + "key": sec.code, "data": { - section.title: section.news_set.filter(is_published=True)[:7], + sec.title: sec.news_set.filter(is_published=True)[:7], } } - for section in sections + for sec in sections ] context = { @@ -161,19 +161,20 @@ def section(request, main): def main_news_list(request): logger.info("正在访问新闻中心") - news_list = News.objects.all() - news_sections = NewsSection.objects.all() + news_list = News.objects.filter(is_published=True).all() + news_sections = NewsSection.objects.filter(visible=True).all() # 分页参数 news_list = custom_pagination(news_list, request) - return render(request, 'front/list.html', { + context = { 'main_section': { 'title': '新闻中心', 'code': 'news' }, 'articles': news_list, 'sections': news_sections, - }) + } + return render(request, 'front/list.html', context) def custom_pagination(news_list, request): @@ -195,7 +196,7 @@ def custom_pagination(news_list, request): def sub_news_list(request, sub: str): logger.info(f"正在访问 {sub}") - sections = NewsSection.objects.all().order_by('order') + sections = NewsSection.objects.filter(visible=True).order_by('order') logger.info(f"新闻板块: {sections}") main_section = { 'title': '新闻中心', diff --git a/server/settings.py b/server/settings.py index 42ab2cf..9f2f076 100644 --- a/server/settings.py +++ b/server/settings.py @@ -109,6 +109,7 @@ TEMPLATES = [ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', + 'base.context_processors.site_info', ], }, }, diff --git a/templates/base.html b/templates/base.html index 5bc5d7a..93da50f 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,26 +1,22 @@ {% load static %} -{% load footer_tags %} - {% block title %}{% endblock %}XXXXXXX官网 - - + {% block title %}{% endblock %}{{ site_info.title }} - -
-

XXXXXXX官网

-

CHANGBAI MOUNTAIN FIRE AND RESCUE BRIGADE

+

{{ site_info.title }}

+

{{ site_info.e_title }}

+

CHANGBAI MOUNTAIN FIRE AND RESCUE BRIGADE

@@ -45,29 +41,13 @@
  • 新闻中心
  • - {% show_main_menu %} + {% for menu in menus %} + - {##} - {# #} - {#
  • #} - {# 信息公开#} - {#
  • #} - {# #} - {#
  • #} - {# 党建引领#} - {#
  • #} - {# #} - {#
  • #} - {# 办事服务#} - {#
  • #} - {# #} - {#
  • #} - {# 互动交流#} - {#
  • #} - {# #} - {#
  • #} - {# 消防科普#} - {#
  • #} + + {% endfor %} @@ -86,75 +66,104 @@ -{% show_footer %} - -{#
    #} -{#
    #} -{#
    #} -{#
    #} -{# #} -{# #} -{# 友情链接#} -{# #} -{#
    #} -{#
    #} -{# #} -{#
    #} -{#
    #} -{# #} -{#
    #} -{#
    #} -{#
    #} -{# #} -{#
    #} +
    + +
    +
    + + + {% for category in categories %} + + +
    + +
    + + {% empty %} + +
    + +
    + {% endfor %} +
    +
    + + +
    + - + +{% block extra_js %}{% endblock %} diff --git a/templates/components/footer.html b/templates/components/footer.html deleted file mode 100644 index 8ae4818..0000000 --- a/templates/components/footer.html +++ /dev/null @@ -1,95 +0,0 @@ -{% load static %} - -
    - -
    -
    - - - {% for category in categories %} - - -
    - -
    - - {% empty %} - -
    - -
    - {% endfor %} -
    -
    - - -
    diff --git a/templates/components/main_menu.html b/templates/components/main_menu.html deleted file mode 100644 index 73ba2e5..0000000 --- a/templates/components/main_menu.html +++ /dev/null @@ -1,10 +0,0 @@ -{% load static %} - -{% for menu in menus %} - - - -{% endfor %} - diff --git a/templates/front/details/news.html b/templates/front/details/news.html index 692e432..1915381 100644 --- a/templates/front/details/news.html +++ b/templates/front/details/news.html @@ -9,8 +9,10 @@ diff --git a/templates/front/index.html b/templates/front/index.html index 866c66b..6879fb7 100644 --- a/templates/front/index.html +++ b/templates/front/index.html @@ -42,7 +42,7 @@ {% include 'components/_carousel.html' with carousel_id=carousel.key slides=carousel.data height="300px" %}
    - {% url "front:news" as more_url %} + {% url "front:news_main" as more_url %} {% include 'components/_news_card.html' with data=news_data.data unique_id=news_data.key more_url=more_url route="front:news_detail" %}
    diff --git a/templates/front/info.html b/templates/front/info.html index 7e2af74..9b6872e 100644 --- a/templates/front/info.html +++ b/templates/front/info.html @@ -4,7 +4,7 @@
    diff --git a/templates/front/list.html b/templates/front/list.html index 5f20fbc..8d1dd2b 100644 --- a/templates/front/list.html +++ b/templates/front/list.html @@ -4,17 +4,24 @@