上传文件:
obj = request.FILES.get('fafa')
import os
file_path = os.path.join('upload', obj.name)
f = open(file_path, mode="wb")
for i in obj.chunks():
f.write(i)
f.close()
return render(request, 'login.html')
前端数据循环输出
数据代码:
USER_DICT = {
'1' : {'name':'root1', 'email' : 'root@qq.com'},
'2' : {'name':'root2', 'email' : 'root@qq.com'},
'3' : {'name':'root3', 'email' : 'root@qq.com'},
'4' : {'name':'root4', 'email' : 'root@qq.com'},
'5' : {'name':'root5', 'email' : 'root@qq.com'},
}
def index(request):
return render(request, 'index.html', {'user_dict': USER_DICT})
前端显示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
{% for row in user_dict.items %}
<li>
{{ row.1.name }}
{{ row.1.email }}
</li>
{% endfor %}
</ul>
</body>
</html>