Django:生成下载链接 [英] Django: generate download link
问题描述
我有一个文件
模型,它在文件系统中存储 path
field-path。所有文件存储在MEDIA_ROOT / files
在模板中,我想为 File
对象生成下载链接。这样做最好的方法是什么?我应该在django中使用静态文件处理吗?
TIA!
UPD / p>
文件
模型
class File(models.Model):
item = models.ForeignKey(Item)
file = models.FileField(upload_to = os.path.join(MEDIA_ROOT,'items'))
format = models.CharField(max_length = 255)
在项目的视图中,我执行以下操作: / p>
files = File.objects.filter(item_id = id)
和通过文件
到模板中
在模板中我使用文件例如.1.file.url
,并且仍然有像 site.com/home/dizpers /...
$ b
UPD2
解决方案
我的问题是在文件模型中,在文件
字段。在upload_to参数中,我使用绝对路径,但必须使用相对路径:
file = models.FileField(upload_to ='items')
我不知道你的意思是生成下载链接,而是简单地链接到该文件,只需使用 {{some_file.url}}
作为您的 href
。
I have a File
model, which store path
field - path in the filesystem to file. All files are stored in MEDIA_ROOT/files
In template I want generate download link for File
object. What is the best way to do this? Should I use static file handling in django?
TIA!
UPD
File
model
class File(models.Model):
item = models.ForeignKey(Item)
file = models.FileField(upload_to = os.path.join(MEDIA_ROOT,'items'))
format = models.CharField(max_length = 255)
In item's View I do the following:
files = File.objects.filter(item_id = id)
and the pass files
to the template
in template I use files.1.file.url
for example and still have bad url like site.com/home/dizpers/...
UPD2
Solution
My problem was in File model, in file
field. In upload_to parameter I use absolute path, but must use relative path:
file = models.FileField(upload_to = 'items')
I'm not sure what exactly you mean by "generate download link", but to simply link to the file, just use {{ some_file.url }}
as your href
.
这篇关于Django:生成下载链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!