LazerFriends
LazerFriends

Reputation: 421

Document link on front end in Wagtail

If I upload a document to the Wagtail CMS, how can I make it available on the front end for download? Is there a specific template tag?

Upvotes: 3

Views: 3085

Answers (1)

gasman
gasman

Reputation: 25227

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 }}">.)

Upvotes: 8

Related Questions