Display PDF File In Django Templates (FileField)

786 Views Asked by At

models.py

class Summary(models.Model):
    name = models.CharField(max_length=500)
    author = models.ForeignKey(Author, on_delete=models.CASCADE, null=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    description = models.TextField(null=True)
    text = RichTextField()
    image = models.ImageField(upload_to='images/%y/%m/%d', null=True)
    pdf = models.FileField(upload_to='pdf', null=True)
    pages = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

views.py

def summary_pdf(request, pk):
     summary = Summary.objects.get(id=pk)

     if not request.user.is_authenticated:
         return redirect('login')

     context = {'summary': summary}
     return render(request, 'main/summary_pdf.html', context)

summary_pdf.html

<body>
   <embed src="{{ summary.pdf.url }}" width="800px" height="900" />
</body>

The problem: enter image description here

Can someone help me? I've been stuck in this since morning and haven't found a solution.

1

There are 1 best solutions below

1
Seyedmahdi moosavyan On

I think you have an indentation error in your models.py

you should add 4 spaces before the line where you set the text attribute.

If this solution does not work, check your runserver terminal. Because if the problem is in PDF rendering and DEBUG is true in the settings.py file, Django should not show you the page whose photo you entered.