0

I am trying to embed a pdf in a Django template. I am using the same way we are embedding an image in django. It`s seems that the file cannot be found. Is there a different way to display it?

Many Thanks

views.py

class BillDetailView(generic.DetailView):
    model = Bill
    template_name = 'accounting/bills/bill_detail.html'

models.py

class BillFile(models.Model):
    bill = models.ForeignKey(Bill,on_delete=models.CASCADE, blank=True, null=True, unique=False)
    bill_file = models.FileField(upload_to='media/bill_files/', default='bill_files/default_id.jpg', blank =True)

bill_detail.html

<div class="card-block">
    {% if billfile.bill_file %}
    <div class="top-right"></div> 
<!-- Option 1 -->

         <embed src="{{ billfile.bill_file.url }}" width = "200" height = "240" style =" margin-left: auto; margin-right: auto; display: block;">Click me</a>

<!-- Option 2 -->      

         <iframe src="{{ billfile.bill_file.url }}" style="width: 100%;height: 100%;border: none;"></iframe>

<!-- Option 3 -->

         <a href="{% static '{{ billfile.bill_file }}' %}">Click me</a>
         {% else %}
         <h3>There is no file to display</h3>                                                               
         {% endif %}                                                                                          
</div>
Steve
  • 31
  • 12
  • Looks like duplicate: https://stackoverflow.com/questions/39919012/django-python-show-pdf-in-a-template – Adam Feb 24 '21 at 15:16
  • Does this answer your question? [Django/Python: Show pdf in a template](https://stackoverflow.com/questions/39919012/django-python-show-pdf-in-a-template) – Adam Feb 24 '21 at 15:16
  • Thanks Adam, it works just by adding an iframe. I modified my code to put the file directly in the bill model as for some reason, I don`t manage to display it when it`s a foreignkey. – Steve Feb 24 '21 at 16:56

0 Answers0