How to return static files passing through a view in django?

前端 未结 8 1807
名媛妹妹
名媛妹妹 2020-12-09 09:04

I need to return css files and js files according to specific logic. Clearly, static serve does not perform what I need. I have a view, whose render method uses logic to fin

8条回答
  •  借酒劲吻你
    2020-12-09 10:00

    you can use below code in your view:
    Note:in this function I return images but you can return every thing based your need and set your context_type

    from django.http import HttpResponse,Http404
    import os
    
    def img_finder(request, img_name):
        try:
            with open(os.path.dirname(os.path.abspath(__file__)) + '/static/img/' + img_name, 'rb') as f:
                return HttpResponse(f.read(), content_type="image/jpeg")
        except IOError:
            raise Http404
    

提交回复
热议问题