【问题标题】:Embeded pdf File is not displaying in Django嵌入式 pdf 文件未在 Django 中显示
【发布时间】:2020-07-18 09:52:25
【问题描述】:

我试图在 django 项目中使用 <embed> 显示照片和 pdf 文件,但 pdf 文件未显示 这是我在做什么 这是视图函数

@login_required
def view_documents(request):
    students = Student.objects.filter(user=request.user)
    return render(request, 'student/view_documents.html',{'students':students})

然后我在模板中使用了标签

{% for student in students %}
#lines of code
{% endfor %}

并显示我使用过的 pdf 文件

<embed src="{{student.adhar_card_pdf.url}}" width="800px" height="400px" type="application/pdf"/>

但它显示了某种错误,例如 localhost 拒绝连接 我也用过 iframe

 <iframe src="{{student.adhar_card_pdf.url}}" style="width:718px; height:700px;" frameborder="0"></iframe>

但是图像在浏览器中显示使用相同的代码没有任何错误

<embed src="{{student.photo_jpg.url}}" width="230px" height="250px" />

为什么不显示pdf文件??请帮我 提前谢谢。 已编辑:在控制台中显示-拒绝在框架中显示“”,因为它将“X-Frame-Options”设置为“拒绝”。 This is how it is displaying pdf

【问题讨论】:

  • 打开您的浏览器开发工具,告诉我们您看到的确切错误。查看网络选项卡并检查它试图获取的 url。查看您的服务器控制台并告诉我们您在那里看到的内容。
  • 在控制台中显示:拒绝在框架中显示“”,因为它将“X-Frame-Options”设置为“拒绝”。

标签: python html django iframe embed


【解决方案1】:
X_FRAME_OPTIONS = 'SAMEORIGIN'

在 settings.py 中的 MIDDLEWARE 部分之前插入上述行 并且对于嵌入标签中的文件路径写入路径为

{{student.file.get.photo_jpg.url}}

然后在 Chrome 或您正在使用的任何浏览器中进行硬重置。

它对我有用,我希望它也对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多