[Answer]-Handling all website one-off root static files like favicon.ico reliably and elegantly?

1๐Ÿ‘

I never get a 500 error from invalid urls, because I use STATIC_URL and the class based RedirectView

Usually I have an app dedicated to this, and include it in the root urls.py with

#urls.py
include('oneoff.urls', name='oneoff')`

and

#oneoff/urls.py
urlpatterns = ( '',
    url(r'favicon.ico$',
        RedirectView.as_view(url=urlparse.urljoin(settings.STATIC_URL, "img/favicon.ico")),
        name="favicon"
    ),
    url(r'icon-precomposed.png',
        RedirectView.as_view(url=urlparse.urljoin(settings.STATIC_URL, "img/iphone/icon.png")),
        name="iphone"
    ),
)

then in the template

{% load url from future %}
<link rel="favicon" href="{% url 'oneoff:favicon' %} />
๐Ÿ‘คThomas Grainger

Leave a comment