[Django]-Document link on front end in Wagtail

7👍

Once you have a reference to the document object, its .url property will give you the correct URL for downloads:

<a href="{{ document.url }}">{{ document.title }}</a>

As for how you get that reference in the first place – usually you’d do it by associating it with a foreign key to wagtaildocs.Document, in much the same way that the tutorial shows associating images with pages:

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')
    ]

(in this case, you would then refer to the document within the template as page.related_document, e.g. <a href="{{ page.related_document.url }}">.)

👤gasman

Leave a comment