def index(request): obj=models.USER.objects.all() fileroot = 'd:\machangwei' fli = os.listdir(fileroot) print(fli) dirdic = {} filedic = {} for i in fli: file = os.path.join(fileroot, i) if os.path.isdir(file): dirdic[i] = file print('d:', file) else: filedic[i] = file print('f:', file) return render(request,'index.html',{"obj":obj,'dirdic':dirdic,'filedic':filedic})视图函数
<h2>根目录下的目录和文件:</h2> {% for k,v in dirdic.items %} <a href="" srcmcw="{{ v }}">{{ k }}</a><br> {% endfor %} {% for k,v in filedic.items %} <a href="" srcmcw="{{ v }}">{{ k }}</a><br> {% endfor %}前端展示
访问结果