Handling all website one-off root static files lik

2019-09-07 06:57发布

问题:

Perhaps I'm missing something (I hope I am!), but it seems awfully clunky to have to have deal with the one-off root website files requested by the browser, such as favicon.ico and things like apple-touch-icon-precomposed.png (on the iPad). Right now, I'm getting a 500 server error whenever I encounter one of these for which I'm not explicitly serving up a file.

My questions:

  1. Is there an up-to-date list of all such files for all major browsers that my webapp should be handling?

  2. As a failsafe, is there way for the absence of any of these files to fail silently, i.e. to NOT get a 500 server error with the webapp continuing on its merry way, simply using a blank favicon or whatever?

I'm running a Django app on Heroku with gunicorn.

回答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' %} />