admin管理员组

文章数量:1125579

In wagtail I have uploaded a document to admin/Documents How in python code can I generate a link to download the document I tried generating a link such as

/documents/<document_id>/<document.pdf>

but I am getting access denied errors in Wagtail template

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>5QNKMKYX4M4TW71A</RequestId>
<HostId>WFI1UWghb12TK+/PKPWIERNfCUTPX3qSGFPMHmb/bJnv8kcqQ5M3ctG0MrjZeum4rzIJbtyHIcU=</HostId>
</Error>

In wagtail I have uploaded a document to admin/Documents How in python code can I generate a link to download the document I tried generating a link such as

/documents/<document_id>/<document.pdf>

but I am getting access denied errors in Wagtail template

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>5QNKMKYX4M4TW71A</RequestId>
<HostId>WFI1UWghb12TK+/PKPWIERNfCUTPX3qSGFPMHmb/bJnv8kcqQ5M3ctG0MrjZeum4rzIJbtyHIcU=</HostId>
</Error>
Share Improve this question asked 2 days ago DavidUDavidU 897 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

If a file is uploaded to a field on your model, for example

from wagtail.admin.panels import FieldPanel
from wagtail.documents import get_document_model
from wagtail.models import Page
    
class HomePage(Page):
    custom_document = models.ForeignKey(
        get_document_model(),
        null=True
        blank=True
        on_delete=models.SET_NULL
        related_name='+'
    )
    
    content_panels = Page.content_panels + [
        FieldPanel('custom_document'),
    ]

In this case you can access url: page.custom_document.url

If you want to know the path to the document directly, that depends on your settings. See Wagtail documentation here for user-uploaded content and also here on storing and serving files.

I my case I added this to my url patterns:

urlpatterns.append(path('wagtail_documents/', include(wagtaildocs_urls)))

So I know I store my documents here:

wagtail_documents/id/filename.doc

The error you show indicates S3 is requiring an authorization token to serve your file. This is exactly as it should be. If you allow documents to be "public-read" in S3, then there is backdoor access that renders the document privacy options useless.

Do you want all documents to download? or do you want some, such as PDFs, to display in the browser? If you want all to download, I would start by setting WAGTAILDOCS_INLINE_CONTENT_TYPES to an empty list. That may only work for references within rich text. If so, you may need to set the WAGTAILDOCS_SERVE_METHOD to "serve" and override the document serve method to set the content disposition header to attachment.

本文标签: Wagtail download link for documentStack Overflow