diff --git a/article/admin.py b/article/admin.py index fd36a38..d0c3d55 100644 --- a/article/admin.py +++ b/article/admin.py @@ -83,18 +83,18 @@ class ArticleForm(forms.ModelForm): super().__init__(*args, **kwargs) # 核心代码:过滤 section 字段,只保留有父级的分类(即二级分类) # 这里的 queryset 会直接作用于 Admin 的下拉选择框 - self.fields['section'].queryset = MainMenu.objects.filter(parent__isnull=False, enabled=True) + self.fields['section'].queryset = MainMenu.objects.filter(parent__isnull=False, visible=True) # 可选:优化显示格式,让用户知道是哪个主板块下的子板块 # 例如显示:"[新闻中心] 国内新闻" - self.fields['section'].label_from_instance = lambda obj: f"[{obj.parent.name}] {obj.name}" + self.fields['section'].label_from_instance = lambda obj: f"[{obj.parent.title}] {obj.title}" # —————— 文章 —————— @admin.register(Article) class ArticleAdmin(ImportExportModelAdmin): form = ArticleForm # <--- 关联自定义表单 - list_display = ('title', 'section', 'author', 'is_published', 'created_at') + list_display = ('title', 'section', 'author', 'is_published', 'created_at', 'attachment_count') list_filter = ('section', 'is_published', 'created_at') search_fields = ('title', 'content') date_hierarchy = 'created_at' @@ -108,6 +108,17 @@ class ArticleAdmin(ImportExportModelAdmin): ('发布设置', {'fields': ('is_published',)}), ) + # 2. 定义显示附件数量的方法 + def attachment_count(self, obj): + # 使用 count() 方法统计数量 + count = obj.attachments.count() + if count == 0: + return "-" # 没有附件显示横杠 + return f"📎 {count}" # 有附件显示图标和数量 + + # 3. 设置该列在后台的标题 + attachment_count.short_description = "附件" + # —————— 通知(带签收) —————— @admin.register(Notice) diff --git a/article/seeder.py b/article/seeder.py index 11e2e91..ab28d15 100644 --- a/article/seeder.py +++ b/article/seeder.py @@ -39,8 +39,48 @@ def seed_articles(count=20): section = random.choice(subsections) author = random.choice(users) if users else None - paragraphs = fake.paragraphs(nb=random.randint(3, 8)) - content_html = "\n".join([f"
{p}
" for p in paragraphs]) + # --- 2. 构建丰富的 HTML 内容 --- + html_parts = [] + + # A. 开头:一段引言或摘要 + if random.random() > 0.5: + html_parts.append(f"【摘要】 {fake.sentence(nb_words=10)}
") + + # B. 正文第一段 + html_parts.append(f"{fake.paragraph(nb_sentences=4)}
") + + # C. 随机插入图片占位符 (模拟上传的封面或插图) + # 使用 placehold.co 生成带文字的灰色占位图 + img_width = random.choice([600, 800]) + img_height = random.choice([300, 400]) + img_text = fake.words(nb=3, unique=True) + html_parts.append( + f'{fake.paragraph(nb_sentences=5)}
") + + # E. 随机插入引用块 (增加排版层次感) + if random.random() > 0.6: + quote = fake.sentence(nb_words=8) + html_parts.append( + f'"{quote}"') + + # F. 随机插入无序列表 (模拟要点陈述) + if random.random() > 0.7: + list_items = "".join([f"
{p1} {fake.words(nb=4, unique=True)} {p2}
") + + # H. 结尾 + html_parts.append(f"{fake.paragraph(nb_sentences=3)}
") + + # 拼接最终 HTML + content_html = "\n".join(html_parts) article = Article.objects.create( title=fake.sentence(nb_words=random.randint(5, 10))[:-1], @@ -81,4 +121,4 @@ def seed_articles(count=20): if __name__ == "__main__": - seed_articles(30) + seed_articles(120) diff --git a/base/admin.py b/base/admin.py index 2241a97..30023fd 100644 --- a/base/admin.py +++ b/base/admin.py @@ -23,7 +23,7 @@ class MainMenuAdmin(ImportExportModelAdmin): display_name.admin_order_field = 'name' def is_enabled(self, obj): - return obj.enabled + return obj.visible is_enabled.boolean = True is_enabled.short_description = '启用' diff --git a/base/templatetags/footer_tags.py b/base/templatetags/footer_tags.py index 35de31c..31eea3c 100644 --- a/base/templatetags/footer_tags.py +++ b/base/templatetags/footer_tags.py @@ -23,8 +23,9 @@ def show_footer(): } -@register.inclusion_tag('components/main_menu.html') -def show_main_menu(): +@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') @@ -33,4 +34,5 @@ def show_main_menu(): # 3. 将数据传递给模板 return { 'menus': menus, + 'request': request, } diff --git a/front/urls.py b/front/urls.py index 59e4ea9..03c4bcb 100644 --- a/front/urls.py +++ b/front/urls.py @@ -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/{p}
" for p in paragraphs]) + # --- 2. 构建丰富的 HTML 内容 --- + html_parts = [] - # 创建新闻对象 + # A. 开头:一段引言或摘要 + if random.random() > 0.5: + html_parts.append(f"【摘要】 {fake.sentence(nb_words=10)}
") + + # B. 正文第一段 + html_parts.append(f"{fake.paragraph(nb_sentences=4)}
") + + # C. 随机插入图片占位符 (模拟上传的封面或插图) + # 使用 placehold.co 生成带文字的灰色占位图 + img_width = random.choice([600, 800]) + img_height = random.choice([300, 400]) + img_text = fake.words(nb=3, unique=True) + html_parts.append( + f'{fake.paragraph(nb_sentences=5)}
") + + # E. 随机插入引用块 (增加排版层次感) + if random.random() > 0.6: + quote = fake.sentence(nb_words=8) + html_parts.append( + f'"{quote}"') + + # F. 随机插入无序列表 (模拟要点陈述) + if random.random() > 0.7: + list_items = "".join([f"
{p1} {fake.words(nb=4, unique=True)} {p2}
") + + # H. 结尾 + html_parts.append(f"{fake.paragraph(nb_sentences=3)}
") + + # 拼接最终 HTML + content_html = "\n".join(html_parts) + + # --- 3. 创建对象 --- news = News.objects.create( - title=fake.sentence(nb_words=random.randint(4, 8))[:-1], # 生成标题并去掉末尾句号 + title=fake.sentence(nb_words=random.randint(4, 8))[:-1], section=section, author=author, content=content_html, - is_published=True, # 默认已发布 - # 封面图留空,因为自动生成图片文件比较复杂,建议手动上传测试图 + is_published=True, + # 可以在这里加上摘要字段,如果有的话 + # summary=fake.paragraph(nb_sentences=1) ) created_count += 1 - print(f" - [{section.name}] {news.title}") + print(f" - [{section.title}] {news.title}") - print(f"✅ 成功生成 {created_count} 条新闻数据!") + print(f"✅ 成功生成 {created_count} 条富文本新闻数据!") if __name__ == "__main__": - seed_news(30) + seed_news(120) diff --git a/templates/components/_news_card.html b/templates/components/_news_card.html index 4f9ced9..fb7ff85 100644 --- a/templates/components/_news_card.html +++ b/templates/components/_news_card.html @@ -47,7 +47,7 @@ {% if items %} {% for item in items %}