Serving /favicon.ico with Django without HTTP redirection 2018-03-29

Say you have created a favicon.ico for your website and want to serve that file outside of the usual /static/images prefix, at /favicon.ico. Other favicon approaches use a code-30x redirect on HTTP level. I would rather save that extra request. Here is what I ended up with:

import os

from django.conf import settings
from django.views.static import serve

urlpatterns += [
    url(r'^(?P<path>favicon\.ico)$', serve, name='favicon',
        kwargs={'document_root': os.path.join(settings.STATIC_ROOT,
                                              'images')}),
]

Licensed under CC0 1.0 Public Domain Dedication.

Leave a Reply

You must be logged in to post a comment.