如果文件退出,但它始终显示Not Avaialable,我正在尝试提供下载选项即使该文件存在。我在views.py中创建了一个字典,如果文件存在,它将为该索引保存True。我还生成了日志,并且在os.path.join上生成的路径是正确的,并且dictionary对于这些值具有True。我认为问题是我在访问模板中的字典时使用2点运算符。
模板
{% for upload in upload_list %}
<tr>
{%if file_list.upload.upload_report_date %}
<td><a href="{%static 'media/daily-{{ upload.upload_report_date|date:"Y-m" }}.csv" download >Download</a></td>
{% else %}
<td>Not Available</td>
{% endif %}
</tr>
{% endfor %}
Views.py
upload_list = Upload.objects.all().order_by('-upload_at')
file_list={}
for upload in upload_list:
try:
if os.path.exists(os.path.join(settings.MEDIA_ROOT,'daily-%s.csv' % (upload.upload_report_date).strftime("%Y-%m"))):
file_list[upload.upload_report_date]=True
except:
pass
您当前正在尝试从模板内访问字典file_list
:file_list.uplad.upload_report_date
。
有了这个,您将永远落在else
,因为您不能那样访问。您的代码尝试获取upload
的属性file_list
,由于该属性不存在,该属性将始终返回None
。
您可以做的是创建可用文件列表(因为您已经调用了变量_list
:]
file_list = []
for upload in upload_list:
try:
if os.path.exists(...):
file_list.append(upload.upload_report_date)
except:
pass
然后在您的模板中:
{% if upload.upload_report_date in file_list %}
...