在django中显示pdf文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在django中显示pdf文件相关的知识,希望对你有一定的参考价值。
嗨,我是django框架的新手当用户点击我需要显示我正在使用的代码的pdf文件
with open(file_path+file_name, 'rb') as pdf:
response = HttpResponse(pdf.read(), mimetype='application/pdf', contenttype='application/pdf')
response['Content-Disposition'] = 'inline;filename=some_file.pdf'
我得到的错误是
File "/usr/local/lib/python3.4/dist-packages/rest_framework/viewsets.py", line 90, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/rest_framework/views.py", line 489, in dispatch
response = self.handle_exception(exc)
File "/usr/local/lib/python3.4/dist-packages/rest_framework/views.py", line 449, in handle_exception
self.raise_uncaught_exception(exc)
File "/usr/local/lib/python3.4/dist-packages/rest_framework/views.py", line 486, in dispatch
response = handler(request, *args, **kwargs)
File "/home/tritwdapp/tribalwelfare/inspection/inspectdataview.py", line 290, in getSubmitReport
response = HttpResponse(pdf.read(), mimetype='application/pdf',contenttype='application/pdf')
File "/usr/local/lib/python3.4/dist-packages/django/http/response.py", line 283, in __init__
super(HttpResponse, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'contenttype'
谁能帮帮我吗
答案
试试这个:
def pdf_view(request):
with open('file.pdf', 'r') as pdf:
response = HttpResponse(pdf.read(), content_type='application/pdf')
response['Content-Disposition'] = 'inline;filename=some_file.pdf'
return response
mimetype
已被content_type
取代。它被删除在Django 1.7
。另外,你拼错了。请注意,这样的解决方案并非最佳。通常,出于性能原因,您的Web服务器将提供这些文件。
以上是关于在django中显示pdf文件的主要内容,如果未能解决你的问题,请参考以下文章