Django通过HttpResponse如何返回用户头像

时间:2022-01-23 22:43:17

代码示例:

def test(request):
# 获取当前文件的路径、父路径以及下一层的路径
curr_dir = os.path.dirname(__file__)
parent_path = os.path.dirname(curr_dir)
image_path = os.path.join(parent_path,"static/image/timg.jpg")
"""
D:\Python Work Location\WebRobot\app01
D:\Python Work Location\WebRobot
D:\Python Work Location\WebRobot\static/image/back.png
"""

image_data = open(image_path,"rb").read()

return HttpResponse(image_data,content_type='image/jpg')

效果展示:
Django通过HttpResponse如何返回用户头像
注意二进制串与utf-8格式的区别:
Django通过HttpResponse如何返回用户头像