我想知道如何在我的模板中显示pdf。我正在使用 python 库请求从外部 API 获取响应。在 get 请求中,API 向我发送了一个 pdf,但我不知道如何处理它以在模板中显示它。
通过
设置内容类型,可以使用 HttpResponse 对象从视图将 pdf 返回到模板,如下面的代码片段所示:
def pdf_response(request):
html_template="Your template path"
#sending the PDF back as response
response=HttpResponse(pdf_file,content_type='application/pdf')
response['Content-Disposition']='filename="test.pdf"'
return response
然后在 urls.py 文件中定义相应的 URL
url(r'^pdf/$',views.pdf_response, name='pdf'),
比在你的模板中定义这样的东西:
PDF Details</h1><a href="{% url 'pdf' %}" class="btn btn-primary">Download</a>