Django ImageField的Upload_Path_Handler文件路径无法检索HTML文件中的图像

时间:2017-12-09 05:39:37

标签: python html django django-models

我尝试使用 Upload_Path_Handler 以便在保存上传的图片文件时允许动态文件夹名称​​(例如上传日期,商品ID等)由用户,以防止重复的实例。

问题是我返回的目录太“深”,无法通过HTML文件检索,因为它以应用程序的名称开头(在本例中为“vault”)。为HTML文件中的图像提供的损坏路径如下:

fullDateRangeList:0
attendanceList:0
attendanceListValues:Value1
attendanceListValues:Value2
attendanceListValues:Value3
attendanceListValues:Value4
.
.
.
attendanceListValues:ValueN

但是,通过使用inspector元素在开头排除应用名称'vault',我能够成功检索图像:

<img src="vault/static/vault/images/recordimages/r_1/2017-12-09_03-27-42/300x300.jpg">

损坏的路径在原始html文件中提供为:

<img src="/static/vault/images/recordimages/r_1/2017-12-09_03-27-42/300x300.jpg">

引用的模型w_image在models.py中如下:

<img src="{{ record.w_image }}">

然后,当然是upload_path_handler:

w_image = models.ImageField(null=False, blank=True, verbose_name='Record Image:', upload_to=upload_path_handler)

如果我要在/ static /开始返回路径,那么它会将图像保存在WebsiteName / static /../ ..而不是WebsiteName / vault / static /../ .. (请记住,这里的目的是让/ static /目录成为我们应用程序的子目录,'vault')

如何在不损害路径作为“保险库”子目录的情况下允许图像可检索?

1 个答案:

答案 0 :(得分:0)

您应该编写图片网址,如下所示:

<img src="{{ record.w_image.url }}">