修复 /contact 404:新增 ContactPage 模型 + 线索表单区块

- 新增 apps.core.ContactPage(SEOablePage),复用 COMMON_BLOCKS StreamField,
  挂载于 HomePage 下;同步补充 HomePage.subpage_types 使后台可创建该子页面。
- 新增迁移 apps/core/migrations/0003_contactpage.py。
- 新增幂等管理命令 seed_contact_page:创建"联系我们"线索表单定义
  (姓名/公司/电话/邮箱/需求描述)+ 创建/更新 slug=contact 的页面,
  接入 bootstrap_deployment 部署引导流程。
- apps/home/tests.py 补充 ContactPage 相关回归测试(subpage_types 集合、
  管理后台可创建性参数化用例),全仓库 pytest 65 -> 68 全部通过。
- 设计文档 §2.16 进度清单同步更新。
This commit is contained in:
2026-09-23 10:24:38 +08:00
parent 18dcc4b584
commit eab27b3454
7 changed files with 148 additions and 2 deletions
@@ -16,6 +16,9 @@
指向它,同时清理 Wagtail 自带的占位欢迎页(已存在则跳过创建,仅确认站点绑定)。
4. 调用 `seed_privacy_policy` 命令:在首页下创建/更新隐私政策页面(依赖第 3 步
已存在的 HomePage)。
5. 调用 `seed_contact_page` 命令:在首页下创建/更新联系我们页面及其线索表单
(依赖第 3 步已存在的 HomePage),修复首页按钮此前链接到 /contact/ 却无
对应页面导致 404 的问题。
安全说明:
- 密码只应通过环境变量/密钥管理服务注入,不会被写入日志。
@@ -39,6 +42,7 @@ class Command(BaseCommand):
call_command("setup_rbac_groups")
call_command("seed_homepage")
call_command("seed_privacy_policy")
call_command("seed_contact_page")
def _bootstrap_superuser(self):
username = os.environ.get("DJANGO_SUPERUSER_USERNAME")
@@ -0,0 +1,78 @@
"""
创建/更新"联系我们"页面(apps.core.ContactPage),挂载在首页下,内嵌一个
"联系我们"线索表单(apps.forms.FormDefinition)。
背景:首页 Hero/CTA 区块的按钮此前一直硬编码链接到 /contact/,但从未创建过对应的
页面(前端也没有 /contact 路由指向的实际内容),导致访客点击后是 404。详见
documents/设计方案分析与完善版.md §2.16。
用法:
python manage.py seed_contact_page
"""
from django.core.management.base import BaseCommand, CommandError
from apps.core.models import ContactPage
from apps.forms.models import FormDefinition, FormDefinitionField
from apps.home.models import HomePage
CONTACT_FORM_NAME = "联系我们"
CONTACT_FORM_FIELDS = [
{"label": "姓名", "field_key": "name", "field_type": "text", "required": True},
{"label": "公司名称", "field_key": "company", "field_type": "text", "required": False},
{"label": "联系电话", "field_key": "phone", "field_type": "tel", "required": True},
{"label": "电子邮箱", "field_key": "email", "field_type": "email", "required": True},
{"label": "需求描述", "field_key": "message", "field_type": "textarea", "required": True},
]
CONTACT_INTRO = "<p>欢迎联系我们,留下您的需求信息,我们的顾问将尽快与您联系。</p>"
class Command(BaseCommand):
help = "创建或更新联系我们页面及其线索表单(挂载于首页下,可重复执行)。"
def handle(self, *args, **options):
home_page = HomePage.objects.live().first() or HomePage.objects.first()
if home_page is None:
raise CommandError(
"未找到 HomePage 实例,请先运行 seed_homepage 创建首页后再运行本命令。"
)
form_def = self._ensure_form_definition()
body = [
("richtext", CONTACT_INTRO),
("form", {"form": form_def}),
]
page = ContactPage.objects.child_of(home_page).filter(slug="contact").first()
if page is None:
page = ContactPage(
title="联系我们",
slug="contact",
body=body,
seo_title_override="联系我们",
seo_description_override="留下您的需求,我们的顾问将尽快与您联系。",
)
home_page.add_child(instance=page)
page.save_revision().publish()
self.stdout.write(self.style.SUCCESS(f"已创建联系我们页面:{page.url}"))
else:
page.body = body
page.save()
page.save_revision().publish()
self.stdout.write(self.style.SUCCESS(f"已更新联系我们页面:{page.url}"))
def _ensure_form_definition(self):
form_def = FormDefinition.objects.filter(name=CONTACT_FORM_NAME).first()
if form_def is not None:
return form_def
form_def = FormDefinition.objects.create(
name=CONTACT_FORM_NAME,
submit_button_text="提交咨询",
success_message="提交成功,我们会尽快与您联系。",
)
for order, field in enumerate(CONTACT_FORM_FIELDS):
FormDefinitionField.objects.create(form=form_def, sort_order=order, **field)
self.stdout.write(self.style.SUCCESS(f"已创建“{CONTACT_FORM_NAME}”线索表单定义。"))
return form_def
File diff suppressed because one or more lines are too long
+27 -1
View File
@@ -12,11 +12,13 @@ from wagtail.admin.panels import FieldPanel, InlinePanel, MultiFieldPanel
from wagtail.api import APIField
from wagtail.contrib.settings.models import BaseSiteSetting
from wagtail.contrib.settings.registry import register_setting
from wagtail.fields import RichTextField
from wagtail.fields import RichTextField, StreamField
from wagtail.models import Orderable, Page
from wagtail.snippets.models import register_snippet
from wagtail_headless_preview.models import HeadlessPreviewMixin
from apps.core.blocks import COMMON_BLOCKS
class SEOablePage(HeadlessPreviewMixin, Page):
seo_title_override = models.CharField(
@@ -77,6 +79,30 @@ class SimpleContentPage(SEOablePage):
verbose_name = "通用内容页"
class ContactPage(SEOablePage):
"""联系我们页面,使用通用组件化 StreamField(可放置 richtext 说明 + form 线索表单区块)。
此前首页 Hero/CTA 按钮一直硬编码链接到 /contact/,但从未创建对应页面,导致 404,
详见 documents/设计方案分析与完善版.md §2.16。
"""
body = StreamField(COMMON_BLOCKS, use_json_field=True, blank=True)
content_panels = SEOablePage.content_panels + [
FieldPanel("body"),
]
api_fields = SEOablePage.api_fields + [
APIField("body"),
]
parent_page_types = ["home.HomePage"]
subpage_types = []
class Meta:
verbose_name = "联系我们页面"
@register_snippet
class TeamMember(models.Model):
"""团队成员,用于关于我们/团队介绍等场景复用。"""
+1
View File
@@ -25,6 +25,7 @@ class HomePage(SEOablePage):
"solutions.SolutionIndexPage",
"cases.CaseStudyIndexPage",
"core.SimpleContentPage",
"core.ContactPage",
]
class Meta:
+3 -1
View File
@@ -9,7 +9,7 @@ import pytest
from apps.blog.models import BlogIndexPage
from apps.cases.models import CaseStudyIndexPage
from apps.core.models import SimpleContentPage
from apps.core.models import ContactPage, SimpleContentPage
from apps.home.models import HomePage
from apps.products.models import ProductIndexPage
from apps.solutions.models import SolutionIndexPage
@@ -24,6 +24,7 @@ def test_home_page_subpage_types_allows_all_top_level_sections():
"solutions.SolutionIndexPage",
"cases.CaseStudyIndexPage",
"core.SimpleContentPage",
"core.ContactPage",
}
assert expected_types.issubset(set(HomePage.subpage_types))
@@ -37,6 +38,7 @@ def test_home_page_subpage_types_allows_all_top_level_sections():
(SolutionIndexPage, {"title": "解决方案", "slug": "solutions"}),
(CaseStudyIndexPage, {"title": "案例", "slug": "cases"}),
(SimpleContentPage, {"title": "隐私政策", "slug": "privacy-policy"}),
(ContactPage, {"title": "联系我们", "slug": "contact"}),
],
)
def test_each_top_level_section_can_be_created_under_home_in_admin(
@@ -409,6 +409,7 @@ CI 中要求单元测试覆盖率不低于 70%,核心 `apps/forms`(涉及线
- [x] 预览模式:后端引入 `wagtail-headless-preview`(0.9.0),`SEOablePage` 混入 `HeadlessPreviewMixin`(自动覆盖全部 6 个页面模型),新增自定义 `PagePreviewAPIViewSet` 挂载于 `/api/v2/page_preview/`;前端新增 `services/preview.service.ts`(`getPreviewOrFallback`/`fetchPreviewPage`/`resolvePreviewPath`)、`/preview`与 `/preview/disable` 两个 Route Handler(基于 Next.js Draft Mode + httpOnly cookie),并在首页/博客/产品/案例/解决方案详情页接入草稿优先逻辑,`layout.tsx` 新增预览模式提示条(含退出链接);`lint`/`build` 均验证通过,后端 31 个现有用例均通过(本次未新增自动化测试,仅依靠人工验证)
- [x] 剩余 6 个 StreamField Block(`ProductCardBlock`/`PricingBlock`/`TimelineBlock`/`TeamBlock`/`TechStackBlock`/`VideoBlock`)已全部开发完成:`apps/core/blocks.py` 新增对应 Block 类并扩充 `COMMON_BLOCKS`(现共 14 个内容 Block + 1 个 `richtext`),因 Wagtail StreamField 会将 Block 结构写入迁移(`use_json_field=True` 不代表可跳过迁移),已为 blog/cases/home/products/solutions 五个 Page 模型生成并应用 `alter field body` 迁移;`TeamBlock` 复用 `FormBlock` 的 `get_api_representation` 覆写模式,新增 `TeamMemberChooserBlock(SnippetChooserBlock)` 展开 `TeamMember` 完整字段(含 `serialize_image` 复用),并补充对应的空值/展开单元测试(新增 2 个用例,后端共 33 个测试全部通过);前端 `types/wagtail.ts` 新增 6 个 Block 值类型与 `StreamFieldBlock` 联合类型分支,新建 6 个渲染组件(`ProductCardGrid`/`Pricing`/`Timeline`/`Team`/`TechStack`/`Video`)并接入 `BlockRenderer.tsx` 映射表,`lint`/`build` 均验证通过
- [x] RBAC 落地:`python manage.py setup_rbac_groups` 幂等命令创建 5 个 Wagtail Group(站点管理员/编辑/审核员/作者/查看者)并按 §2.8 矩阵配置 `GroupPagePermission`/`GroupCollectionPermission`/Snippet 与用户管理 `Permission`;Author“仅自己”利用 Wagtail 内置 `OwnershipPermissionPolicy`(仅 add 权限)原生实现,无需自定义 hook;新增 7 个后端单元测试,全仓库 pytest 从 33 增至 40,全部通过
- [x] `apps.core.ContactPage`:联系我们页面(复用 `COMMON_BLOCKS` StreamField,可放置说明文字 + 线索表单区块),修复了首页 Hero/CTA 按钮此前一直硬编码链接到 `/contact/` 却从未创建对应页面导致的 404;`HomePage.subpage_types` 同步补充 `core.ContactPage`;新增幂等命令 `seed_contact_page`(创建"联系我们"表单定义 + 页面)并接入 `bootstrap_deployment`;前端新增 `/contact` 路由(沿用首页 preview/fallback 模式);新增/调整 3 个测试用例,全仓库 pytest 从 65 增至 68
未完成(待规划排期):
- [ ] 测试覆盖率持续提升中:后端 pytest 用例从 40 个增至 65 个,新增覆盖 `apps/blog`(标签、列表排序/草稿过滤)、`apps/solutions`(页面树、草稿过滤)、`apps/home`(`HomePage.subpage_types` 回归测试,覆盖此前"后台无法添加子页面"的 bug 场景)、`apps/core` 健康检查端点 `/healthz`/`/readyz`、发布后 revalidate webhook 信号(含正常/未配置/网络异常三种路径)、`apps/api` 预览接口 `page_preview`(含无效 token/content_type 场景,过程中顺带修复了一个真实 bug:无效 token 之前会 500 而非文档承诺的 404)、`bootstrap_deployment` 部署引导命令(建号/跳过/不覆盖密码/联动 RBAC);但集成测试、前端组件测试、E2E 均未编写,当前无 CI 流水线