开发者

Serving favicon.ico with Django. Why does settings.MEDIA_URL with django.views.generic.simple.redirect_to only work on dev environment?

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?


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


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" />


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'))
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜