24

I found this solution for serving favicon.ico with django.

(r'^favicon\.ico$',
  'django.views.generic.simple.redirect_to',
  {'url': settings.MEDIA_URL+'images/favicon.ico'}),

I do not understand why it only works for the development server. Going to /favicon.ico works on dev, doesn't with debug=False. It should redirect to /media/images/favicon.ico (served by apache), which does work if you access it directly.

Any ideas?

Paolo
  • 20,112
  • 21
  • 72
  • 113
Clash
  • 4,896
  • 11
  • 47
  • 67

3 Answers3

51

I'd recommend against serving the favicon with django unless you absolutely have to. Instead, putting a setting in your web server config that adds an alias pointing to the favicon.

For example, in apache:

Alias /favicon.ico /path/to/media_url/images/favicon.ico
Jordan Reiter
  • 20,467
  • 11
  • 95
  • 161
  • Agreed. Just went to my shared hosting, got it to work putting an exception for lighttpd (I thought it was apache serving the stuff) – Clash Aug 11 '10 at 17:26
29

This is not direct answer to you question, but you can use this for favicon:

<link rel="shortcut icon" href="{{ STATIC_URL }}img/favicon.ico" />
Sergey Lyapustin
  • 1,917
  • 17
  • 27
  • 2
    That works for the website, but sometimes browsers & tools try to access /favicon.ico directly. – Dustin Nov 15 '13 at 00:16
16

redirect_to has been deprecated in Django 1.5. You can use the class based RedirectView

from django.conf import settings
from django.views.generic import RedirectView

urlpatterns = patterns('',
    (r'^favicon\.ico$', RedirectView.as_view(url=settings.MEDIA_URL + 'images/favicon.ico'))
)
Community
  • 1
  • 1
Eran Duchan
  • 381
  • 3
  • 6
  • hmm for me it works in firefox, but not in Chrome. What can be the reason? I cleared cache of course.. – andilabs Jul 09 '14 at 21:43
  • 1
    @andi Some software looks for `favicon.ico` in the root of the domain, some follow the `` HTML, and some software looks elsewhere. This site is useful for checking that your favicon is good for most software: http://realfavicongenerator.net/favicon_checker – Flimm Nov 16 '16 at 12:24