# 创建联系页面 在你的作品集网站上放一个联系页面,有助于你与潜在客户、雇主或其他对你技能感兴趣的专业人士建立联系。 在本教程这一节中,你会用 Wagtail 表单给作品集网站添加一个联系页面。 先从修改你的 `base/models.py` 文件开始: ```python from django.db import models # 导入 ParentalKey: from modelcluster.fields import ParentalKey # 导入 FieldRowPanel 和 InlinePanel: from wagtail.admin.panels import ( FieldPanel, FieldRowPanel, InlinePanel, MultiFieldPanel, PublishingPanel, ) from wagtail.fields import RichTextField from wagtail.models import ( DraftStateMixin, PreviewableMixin, RevisionMixin, TranslatableMixin, ) # 导入 AbstractEmailForm 和 AbstractFormField: from wagtail.contrib.forms.models import AbstractEmailForm, AbstractFormField # 导入 FormSubmissionsPanel: from wagtail.contrib.forms.panels import FormSubmissionsPanel from wagtail.contrib.settings.models import ( BaseGenericSetting, register_setting, ) from wagtail.snippets.models import register_snippet # ... 保留 NavigationSettings 和 FooterText 的定义。添加 FormField 和 FormPage: class FormField(AbstractFormField): page = ParentalKey("FormPage", on_delete=models.CASCADE, related_name="form_fields") class FormPage(AbstractEmailForm): intro = RichTextField(blank=True) thank_you_text = RichTextField(blank=True) content_panels = AbstractEmailForm.content_panels + [ FormSubmissionsPanel(), FieldPanel("intro"), InlinePanel("form_fields"), FieldPanel("thank_you_text"), MultiFieldPanel( [ FieldRowPanel( [ FieldPanel("from_address"), FieldPanel("to_address"), ] ), FieldPanel("subject"), ], "Email", ), ] ``` 在上面的代码中,你的 `FormField` 模型继承自 `AbstractFormField`。借助 `AbstractFormField`,你可以在管理界面中定义任意你想要表单字段类型。`page = ParentalKey('FormPage', on_delete=models.CASCADE, related_name='form_fields')` 定义了 `FormField` 与 `FormPage` 模型之间的父子关系。 另一方面,你的 `FormPage` 模型继承自 `AbstractEmailForm`。与 `AbstractFormField` 不同,`AbstractEmailForm` 提供了把表单内容发送到邮件的功能。此外,它还定义了 `to_address`、`from_address` 和 `subject` 字段。它要求定义好 `form_fields`。 定义好 `FormField` 和 `FormPage` 模型之后,你必须创建 `form_page` 和 `form_page_landing` 模板。`form_page` 模板与标准的 Wagtail 模板不同,因为除了通常的 `Page` 变量之外,它还会收到一个名为 `form` 的变量,其中含有一个 Django `Form` 对象。而 `form_page_landing.html` 则是一个标准的 Wagtail 模板。用户成功提交表单后,你的站点会显示 `form_page_landing.html`。 现在,创建 `base/templates/base/form_page.html` 文件,并向其中加入以下内容: ```html+django {% extends "base.html" %} {% load wagtailcore_tags %} {% block body_class %}template-formpage{% endblock %} {% block content %}

{{ page.title }}

{{ page.intro|richtext }}
{% csrf_token %} {{ form.as_div }}
{% endblock content %} ``` 同时,创建 `base/templates/base/form_page_landing.html` 文件,并向其中加入以下内容: ```html+django {% extends "base.html" %} {% load wagtailcore_tags %} {% block body_class %}template-formpage{% endblock %} {% block content %}

{{ page.title }}

{{ page.thank_you_text|richtext }}
{% endblock content %} ``` 现在,你已经把在作品集网站上创建联系页面所需的全部代码和模板都加好了。 现在,运行 `python manage.py makemigrations`,然后运行 `python manage.py migrate` 来迁移你的数据库。 (add_your_contact_information)= ## 添加你的联系信息 要给作品集网站添加联系信息,请按以下步骤操作: 1. 按以下步骤,在 **Home** 下创建一个 **Form page** 作为子页面: a. 重启你的服务器。 b. 进入你的管理界面。 c. 在[侧边栏](https://guide.wagtail.org/en/how-to-guides/find-your-way-around/#the-sidebar)中点击 `Pages`。 d. 点击 `Home`。 e. 在跳转后的页面上方点击 `+` 图标(添加子页面)。 f. 点击 `Form page`。 2. 填入必要的数据。 3. 发布你的 `Form Page`。 ## 美化你的联系页面 要美化你的联系页面,请在 `mysite/static/css/mysite.css` 文件中加入以下 CSS: ```css .page-form label { display: block; margin-top: 10px; margin-bottom: 5px; } .page-form :is(textarea, input, select) { width: 100%; max-width: 500px; min-height: 40px; margin-top: 5px; margin-bottom: 10px; } .page-form .helptext { font-style: italic; } ``` 在本教程的下一节中,你会学到如何给站点添加作品集页面。