django - 我如何重定向以循环遍历 django html 中的多个变量
问题描述
我想根据 i 的值转到不同的页面,我知道语法不正确,但我需要一些建议以另一种方式做同样的事情我是堆栈溢出的新手,如果我的问题很愚蠢,请原谅
{% for document,i in details , flag reversed %}
<tr>
{% if i %}
<td><li><a href="{{ document.docfile.url}}">{{ document.docfile}}</a></li> </td>
{% else %}
<td><li><a href="./{{ whole }}/{{ document.docfile}}">{{ document.docfile}}</a></li> </td>
{% endif %}
<td>{{ document.DateOfUpload}}</td>
<td>{{ document.emailid}}</td>
</tr>
{% endfor %}
这是视图.py
def getAllFiles(request,project_name,wholepath):
wholepath=str(wholepath)
whole=[]
whole=wholepath.split('/')
w=len(whole)
w=w-1
whole=whole[w]
wholepath= wholepath.replace('/', '\\')
wholepath="./media/"+wholepath
if wholepath:
getallfiles=Document.objects.filter(parentdirectory=wholepath)
print(getallfiles)
flag=[]
i=0
for c in getallfiles:
i=i+1;
st=(c.docfile)
st=str(st)
print(st,type(st))
if os.path.isfile(st):
flag.insert(i,"1")
else:
flag.insert(i,"0")
return render(request, 'FileView.html', {'details': getallfiles,'whole': whole, 'flag': flag})
else:
getallfiles=Document.objects.all()
return render(request, 'AllUploads.html', {'details': getallfiles,'whole':whole})
网址.py
path('projectlist/<slug:project_name>/<path:wholepath>', views.getAllFiles),
如果它是一个文件,那么 url 应该看起来像 ./media/filelocation 或者如果它是一个目录,那么它应该添加以前的路径
解决方案
利用zip_longest
例如,我有 3 个列表:
from itertools import zip_longest
list1=[1,2,3]
list2=['a','b','c']
list2=[5,6,7]
list=list(zip_longest(list1,list2,list3))
for x,y,z in list:
print(x,y,z)
输出:
1 a 5
2 b 6
3 c 7
推荐阅读
- reactjs - 服务器端渲染的 Nextjs Bootstrap Dropdown 问题
- javascript - 我无法使用 webpack --mode=development 或 --mode=production (Node js) 构建 Javascript
- node.js - Fastify Typescript 请求查询
- netlogo - NetLogo 中的 SEIS 疾病模型帮助 - 受感染的个体不会再次变得易感?
- kendo-ui-angular2 - 在 kendo UI 角度网格中显示来自嵌套 json 的数据
- coq - 比较总和 ssreflect
- linux - 为 SIM7600A 构建驱动程序时没有制定目标的规则
- sparql - PLS 帮助修复 SPARQL 查询
- javascript - IndexedDB - 在升级期间强制中止,但有承诺
- java - 同一个jtable中2个不同id的比较