(documents_overview)=
# 文档概览
本页概述在你的 Wagtail 项目中使用 `'wagtail.documents'` 应用的基础知识。
## 在 `INSTALLED_APPS` 中包含 `'wagtail.documents'`
要使用 `wagtail.documents` 应用,你需要把它包含在 Django 项目设置的 `INSTALLED_APPS` 列表中。像这样把它加入列表即可:
```python
# settings.py
INSTALLED_APPS = [
# ...
"wagtail.documents",
# ...
]
```
## 设置 URL
接下来,你需要为 `wagtail.documents` 应用设置 URL。你可以把这些 URL 包含在项目的主 urls.py 文件中。为此,加入以下代码行:
```python
# urls.py
from wagtail.documents import urls as wagtaildocs_urls
urlpatterns = [
# ...
path("documents/", include(wagtaildocs_urls)),
# ...
]
```
新保存的文档默认会存入[引用索引](managing_the_reference_index)。
## 在页面中使用文档
要在 Wagtail 页面中包含文档文件,你可以在页面模型中使用 [`FieldPanel`](wagtail.admin.panels.FieldPanel)。
下面是一个例子:
```python
# models.py
from wagtail.admin.panels import FieldPanel
from wagtail.documents import get_document_model
class YourPage(Page):
# ...
document = models.ForeignKey(
get_document_model(),
null=True,
blank=True,
on_delete=models.SET_NULL,
)
content_panels = Page.content_panels + [
# ...
FieldPanel("document"),
]
```
这样你在创建或编辑页面时就可以选择一个文档文件,并在页面模板中链接到它。
下面是一个访问文档字段并渲染它的示例模板:
```html+django
{% extends "base.html" %}
{% block content %}
{% if page.document %}
Document: {{ page.document.title }}
File Type: {{ page.document.file_extension }}
View Document
{% else %}
No document attached to this page.
{% endif %}
{{ page.body }}
{% endblock %}
```
## 在 `RichTextFields` 中使用文档
可以在页面中使用 [`RichTextField`](rich_text_field)创建指向文档的链接。默认情况下,Wagtail 会包含添加文档链接所需的功能,见 [](rich_text_features)。
你可以通过给 `RichTextField` 传入 `features` 来排除或包含这些功能。在下面的示例中,我们创建一个只含文档链接和基本格式的 `RichTextField`。
```python
# models.py
from wagtail.fields import RichTextField
class BlogPage(Page):
# ...other fields
document_footnotes = RichTextField(
blank=True, features=["bold", "italic", "ol", "document-link"]
)
panels = [
# ...other panels
FieldPanel("document_footnotes"),
]
```
## 在 `StreamField` 中使用文档
`StreamField` 提供了一种适合不遵循固定结构页面的内容编辑模型。要在 `StreamField` 中添加指向文档的链接,请把它包含在模型中,并同时包含 `DocumentChooserBlock`。
创建一个带名为 `doc` 的 `StreamField` 的 `Page` 模型,并在该字段内放入名为 `doc_link` 的 `DocumentChooserBlock`:
```python
# models.py
from wagtail.fields import StreamField
from wagtail.documents.blocks import DocumentChooserBlock
class BlogPage(Page):
# ... other fields
documents = StreamField(
[("document", DocumentChooserBlock())],
null=True,
blank=True,
use_json_field=True,
)
panels = [
# ... other panels
FieldPanel("documents"),
]
```
在 `blog_page.html` 中,加入下面这段代码以在页面中显示文档链接:
```html+django
{% for block in page.documents %}
{{ block.value.title }}
{% endfor %}
```
## 文档与集合
Wagtail 中的文档可以在[集合](https://guide.wagtail.org/en/how-to-guides/manage-collections/)中组织。集合提供了一种把相关文档分组的方式。你可以在集合之间交叉链接文档,并让它们可以通过站点的不同部分访问。
下面是一个例子:
```python
from wagtail.documents import get_document_model
class PageWithCollection(Page):
collection = models.ForeignKey(
"wagtailcore.Collection",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
verbose_name="Document Collection",
)
content_panels = Page.content_panels + [
FieldPanel("collection"),
]
def get_context(self, request):
context = super().get_context(request)
documents = get_document_model().objects.filter(collection=self.collection)
context["documents"] = documents
return context
```
下面是一个访问文档集合并渲染它的示例模板:
```html+django
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block content %}
{% if documents %}
Documents:
{% endif %}
{% endblock %}
```
## 把文档设为私有
如果你想限制对某些文档的访问,可以把它们放入[私有集合](https://guide.wagtail.org/en/how-to-guides/manage-collections/#privacy-settings)。
私有集合不公开可访问,其内容只对拥有相应权限的用户可用。
## API 访问
Wagtail 中的文档可以通过 `wagtail.documents.api.v2.views.DocumentsAPIViewSet` 经由 API 访问。这让你可以以程序化方式操作文档、获取其详情并执行各种操作。
更多细节可参阅[API 章节](api_v2_configure_endpoints),其中提供了额外信息和使用示例。
只要启用了 v3 API,[v3 图片 API 端点](api_v3_documents)也可用,并提供诸如“写”操作等更高级的功能。