图片视频加载到页面中
settings.py 文件:
# 配置上传文件的路径 MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
项目下新建一个media目录,加载的的东西会自动保存到里面
views.py 文件中:
def index(request): if request.method == 'GET': stuinfos = StudentInfo.objects.all() return render(request, 'index.html', {'stuinfos': stuinfos})
index.html文件中:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>所有学生信息</title> </head> <body> {% for stuinfo in stuinfos %} 姓名:{{ stuinfo.s.u_name }} 电话:{{ stuinfo.s.u_password }} 地址:{{ stuinfo.s.u_ticket }} 头像: {% if stuinfo.i_img %} <img src="/media/{{ stuinfo.i_img }}" width="100" height="100"> <br> {% endif %} {% endfor %} <h4>-----------------------------我是分割线----------------------------------------</h4> <a href="{% url 's:add'%}">添加学生</a> <a href="/uauth/logout/">退出</a> </body> </html>
实现效果: