1 year ago

#76352

test-img

Finn

Django embed/dispaly .pdf in page

I am attempting to display .pdfs that have been uploaded by users. I can display the path to the pdf but not document itself. I attempted to use "" in the template but this is not working.

At least one issue is that the incorrect path to the document is used when the template is rendered. The correct file path is media/company1/documents/012022/document.pdf. The file path rendered in the template is: /documents/document/4/documents/012022/document.pd

Here is my model:

from django.db import models
from constrainedfilefield.fields import ConstrainedFileField


class Document(models.Model):
    title = models.CharField(max_length= 200)
    description = models.TextField()
    file = ConstrainedFileField(
                            null=True,
                            blank=True,
                            upload_to='documents/%m%Y',
                            content_types=['application/pdf'],
                            max_upload_size=2097152,
                                    )

Here is my view:

from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import ListView, DetailView, CreateView
from .models import Document

...

class CompanyDocumentsDetailView(DetailView):
    model = Document
    template_name = 'company_accounts/document_detail.html'
...

Here is my template:
<!-- templates/document_detail.html -->

{% extends 'base.html' %}

{% block content %}
  <div class="section-container container">

  <div class="project-entry">
    <h2>{{ document.title }}</h2>
    <p>{{ document.description }}</p>
    <p><embed src="{{document.file}}" type="application/pdf"   height="700px"     width="500"/></p>
  </div>

</div>
{% endblock content %}

django

django-templates

0 Answers

Your Answer

Accepted video resources