首页 > 解决方案 > Django:如何在新选项卡中显示 pdf 文件?

问题描述

大多数显示如何在新选项卡中打开 pdf 文件的帖子已有 3 年历史。Django中打开上传到模型的pdf文件的最佳方法是什么?

发票.py

class Invoice(models.Model):
    
    file = models.FileField(upload_to='estimates/', blank =True)
    name = models.CharField(max_length=250, blank =True)    

    def __str__(self):
        return self.name

视图.py

def view_invoice(request, invoice_id):
    invoice = get_object_or_404(Invoice, pk=invoice_id)
    return render(request, 'index_invoice.html', {'invoice': invoice}) 

index_invoice.html

<a href=""><i class="fas fa-eye"></i>&nbsp;</a>

非常感谢

标签: pythondjango

解决方案


您应该能够使用FileField的网址:

<a href="{{ invoice.file.url }}"><i class="fas fa-eye"></i>&nbsp;</a>

推荐阅读