Wagtail 前端的文档链接

Laz*_*nds 3 django wagtail

如果我将文档上传到 Wagtail CMS,如何使其在前端可供下载?是否有特定的模板标签?

gas*_*man 6

一旦您引用了文档对象,它的.url属性就会为您提供正确的下载 URL:

<a href="{{ document.url }}">{{ document.title }}</a>
Run Code Online (Sandbox Code Playgroud)

至于你如何首先获得该引用 - 通常你会通过将它与外键相关联来实现wagtaildocs.Document,这与教程显示的将图像与页面相关联的方式大致相同:

from wagtail.documents.edit_handlers import DocumentChooserPanel

class MyPage(Page):
    # ...
    related_document = models.ForeignKey(
        'wagtaildocs.Document', blank=True, null=True,
         on_delete=models.SET_NULL, related_name='+'
    )

    content_panels = Page.content_panels + [
        # ...
        DocumentChooserPanel('related_document')
    ]
Run Code Online (Sandbox Code Playgroud)

(在这种情况下,您可以将模板中的文档称为page.related_document,例如<a href="{{ page.related_document.url }}">。)