django -页面中加载图片

图片视频加载到页面中

  1. settings.py 文件:

    #  配置上传文件的路径
    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
    
  2. 项目下新建一个media目录,加载的的东西会自动保存到里面

  3. views.py 文件中:

    def index(request):
    
        if request.method == 'GET':
         stuinfos = StudentInfo.objects.all()
         return render(request, 'index.html', {'stuinfos': stuinfos})
    
  4. 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>
    

    实现效果:
    这里写图片描述

猜你喜欢

转载自blog.csdn.net/hello_syt_2018/article/details/80200801