[Fixed]-Django: return image data from a view

14👍

Here is a simple example from the django-openid project

def logo(request):
    return HttpResponse(
        OPENID_LOGO_BASE_64.decode('base64'), content_type='image/gif'
    )
    # Logo from http://openid.net/login-bg.gif
    # Embedded here for convenience; you should serve this as a static file
    OPENID_LOGO_BASE_64 = """
    R0lGODlhEAAQAMQAAO3t7eHh4srKyvz8/P5pDP9rENLS0v/28P/17tXV1dHEvPDw8M3Nzfn5+d3d
    3f5jA97Syvnv6MfLzcfHx/1mCPx4Kc/S1Pf189C+tP+xgv/k1N3OxfHy9NLV1/39/f///yH5BAAA
    AAAALAAAAAAQABAAAAVq4CeOZGme6KhlSDoexdO6H0IUR+otwUYRkMDCUwIYJhLFTyGZJACAwQcg
    EAQ4kVuEE2AIGAOPQQAQwXCfS8KQGAwMjIYIUSi03B7iJ+AcnmclHg4TAh0QDzIpCw4WGBUZeikD
    Fzk0lpcjIQA7
    """
👤KS.Pan

1👍

You may consider using base64. But you might find that the performance boost is almost irrelevant as your image will be stored in the disk cache. I would be interested to see the benchmarks of base64 decoding vs disk access. You may even find that this optimisation would have the opposite effect.

Edit: I found this page on the linux disk cache which you might interesting reading when making this decision. So long as your disk activity isn’t too high, the chances of this tiny image being part of the disk cache seems likely.

Leave a comment