@ wrote... (4 weeks ago)

Sometimes you don't have a favicon.ico or it's not in your staticfiles because you have a single page javascript or you're just tired of seeing 404s in your debug output.

Here's how you can serve up a hard coded icon (or any file really) directly from django.

I got this transparent icon from transparent-favicon.info

wget http://transparent-favicon.info/favicon.ico
base64 favicon.ico

I put this in my main project urls.py but feel free to put wherever.

# project/urls.py

def favicon(request):
    from textwrap import dedent
    from django.http import HttpResponse
    import base64

    icon = """\
    AAABAAEAEBACAAEAAQCwAAAAFgAAACgAAAAQAAAAIAAAAAEAAQAAAAAAgAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAA////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAAAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD/
    /wAA//8AAP//AAD//wAA//8AAP//AAD//wAA"""
    icon = dedent(icon)
    icon = base64.b64decode(icon)

    return HttpResponse(icon, content_type="image/x-icon")

urlpatterns += [
    url(r'^favicon.ico', favicon, name='favicon'),
]
Category: tech, Tags: django
Comments: 0
Click here to add a comment