Wagtail 文档链接下载而不是显示为页面
Wagtail document links downloading instead of displaying as a page
Wagtail CMS 的默认配置似乎是让文档链接触发自动下载文档,而不是在浏览器中显示文档。有没有简单的方法来更改此配置?
下载文档链接非常标准,主要是因为在浏览器中预览文档对于每个浏览器来说确实不同。
您可以添加一个模板过滤器,将 URL 解析为 PDF 并添加 target="_blank"
属性。
对于在线托管的 PDF,这可能适用于大多数浏览器:
如何制作自定义过滤器:
https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/#registering-custom-filters
另一种方法是挂入 before_serve_document
鹡鸰钩。
https://docs.wagtail.io/en/v2.9.3/reference/hooks.html?highlight=document%20serving#document-serving
您可以通过这种方式根据文档自定义响应,这是一个非常粗略的示例,展示了挂钩的工作原理。
您仍然需要解决如何生成显示文件的可视化 URL。
from wagtail.wagtailcore import hooks
from django.shortcuts import redirect
@hooks.register('before_serve_document')
def serve_document(document, request):
# eg. use document.file_extension, document.url, document.filename
if document.file_extension == 'pdf':
google_view_pdf_base = 'https://docs.google.com/viewer?url='
# document.url is a relative URL so more work needed here
# also URL should probably be URL encoded
redirect_url = google_view_pdf_base + document.url
# must return an instance of HTTPResponse
return redirect(redirect_url)
# no return means the normal page serve will operate
根据上面 LB 接受的答案,以下代码将允许您在不依赖 Google 文档查看器的情况下提供要查看的文档。
您还可以通过传递查询字符串参数恢复为标准行为,例如https://.../?download=True
from django.http import HttpResponse
from wagtail.core import hooks
@hooks.register('before_serve_document')
def serve_pdf(document, request):
if document.file_extension != 'pdf':
return # Empty return results in the existing response
response = HttpResponse(document.file.read(), content_type='application/pdf')
response['Content-Disposition'] = 'filename="' + document.file.name.split('/')[-1] + '"'
if request.GET.get('download', False) in [True, 'True', 'true']:
response['Content-Disposition'] = 'attachment; ' + response['Content-Disposition']
return response
在显示 pdf 的 html 文件中,只需在 url 之前添加文件:
例如:{{ file_name.file.url }}
Wagtail CMS 的默认配置似乎是让文档链接触发自动下载文档,而不是在浏览器中显示文档。有没有简单的方法来更改此配置?
下载文档链接非常标准,主要是因为在浏览器中预览文档对于每个浏览器来说确实不同。
您可以添加一个模板过滤器,将 URL 解析为 PDF 并添加 target="_blank"
属性。
对于在线托管的 PDF,这可能适用于大多数浏览器:
如何制作自定义过滤器: https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/#registering-custom-filters
另一种方法是挂入 before_serve_document
鹡鸰钩。
https://docs.wagtail.io/en/v2.9.3/reference/hooks.html?highlight=document%20serving#document-serving
您可以通过这种方式根据文档自定义响应,这是一个非常粗略的示例,展示了挂钩的工作原理。
您仍然需要解决如何生成显示文件的可视化 URL。
from wagtail.wagtailcore import hooks
from django.shortcuts import redirect
@hooks.register('before_serve_document')
def serve_document(document, request):
# eg. use document.file_extension, document.url, document.filename
if document.file_extension == 'pdf':
google_view_pdf_base = 'https://docs.google.com/viewer?url='
# document.url is a relative URL so more work needed here
# also URL should probably be URL encoded
redirect_url = google_view_pdf_base + document.url
# must return an instance of HTTPResponse
return redirect(redirect_url)
# no return means the normal page serve will operate
根据上面 LB 接受的答案,以下代码将允许您在不依赖 Google 文档查看器的情况下提供要查看的文档。
您还可以通过传递查询字符串参数恢复为标准行为,例如https://.../?download=True
from django.http import HttpResponse
from wagtail.core import hooks
@hooks.register('before_serve_document')
def serve_pdf(document, request):
if document.file_extension != 'pdf':
return # Empty return results in the existing response
response = HttpResponse(document.file.read(), content_type='application/pdf')
response['Content-Disposition'] = 'filename="' + document.file.name.split('/')[-1] + '"'
if request.GET.get('download', False) in [True, 'True', 'true']:
response['Content-Disposition'] = 'attachment; ' + response['Content-Disposition']
return response
在显示 pdf 的 html 文件中,只需在 url 之前添加文件:
例如:{{ file_name.file.url }}