Embeded pdf File is not displaying in Django

Viewed 1328

I was trying to display photos and pdf files in a django project using <embed> but pdf file is not displaying Here is what i was doing This is view function

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

and then I used for tag in template

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

and to display the pdf file i have used

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

but it was showing some kind of error like localhost refused to connect i also used iframe

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

But image is displaying in the browser using same code without any error

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

Why pdf file is not displaying ?? Please help me Thanx in advance. Edited: In console it is showing -- Refused to display '' in a frame because it set 'X-Frame-Options' to 'deny'. This is how it is displaying pdf

1 Answers
X_FRAME_OPTIONS = 'SAMEORIGIN'

Insert the above line in settings.py just before the MIDDLEWARE section and for the file path in embed tag write path as

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

And then give a hard reset in Chrome or any browser you are using.

It worked for me I hope it works for you as well.

Related