Python Django使用HttpResponse返回圖片並顯示


views.py

    def read_img(request):
        """
        : 讀取圖片
        :param request:
        :return:
        """
        try:
            data = request.GET
            file_name = data.get("file_name")
            imagepath = os.path.join(settings.BASE_DIR, "static/resume/images/{}".format(file_name))  # 圖片路徑
            with open(imagepath, 'rb') as f:
                image_data = f.read()
            return HttpResponse(image_data, content_type="image/png")
        except Exception as e:
            print(e)
            return HttpResponse(str(e))

 

urls.py

url(r'^read_img/(?P<news_id>.+)/$',views.read_img,name="image"),

 

模板調用

<img src="{% url 'image' param.id %}" alt="{{param.id}}"/>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM