python - 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> </a>
非常感谢
解决方案
您应该能够使用FileField
的网址:
<a href="{{ invoice.file.url }}"><i class="fas fa-eye"></i> </a>
推荐阅读
- typescript - TypeScript 编译器 API:查找类型来自哪个文件
- flask - 将值传递给 Bootstrap4 弹出框
- node.js - 使用 sendgrid 时,gmail 中不存在的图像显示为“图像不显示”
- php - PHP:PDO 更新记录不工作(无错误)
- c - 传递给函数时指针值意外更改
- r - R如何提取基于n-gram的行
- java - 我不确定 getFlightTime() 方法该怎么做,如何以毫秒为单位获取时间?
- react-native - 通过 react-saga 收到成功操作后,我可以更改应用程序屏幕吗?
- elasticsearch - 查询 elasticsearch 可以通过 id 获取文档,但在查询 all 中找不到
- c# - 以隐藏方式启动 IE 进程