Search code examples
pythondjango

How to get my favicon at `example.com/favicon.ico` in my Django project?


Google currently does not show my website's favicon in its search results and I learned that it is becuase it should be located at example.com/favicon.ico. I'm looking for a simple way to do this, hopefully without relying on redirects. I've used redirection to the version in my static folder but when I visit the url it redirects to example.com/static/favicon.ico, which I do not want. I want that if a user or a crawler visits example.com/favicon.ico they see my favicon. It is currently located at:

my_project/
├── my_project/
│   ├── ...
│   ├── settings.py
│   ├── ...
├── manage.py
└── static/
    └── img/
        └── favicon.ico

I use gunicorn as my web server and whitenoise as my http server. Here is my urls.py:

from django.urls import path, include  # new

from django.contrib.sitemaps.views import sitemap  # new

from finder.views import ProcessorDetailSitemap, ArticleDetailSitemap, StaticViewSitemap  # Correct import path


#from django.conf import settings

#from django.conf.urls.static import static


sitemaps = {

    'processor':ProcessorDetailSitemap,

    'article': ArticleDetailSitemap,

    'static': StaticViewSitemap,

}


urlpatterns = [

    path('admin/', admin.site.urls),

    path('', include('finder.urls')), #new

    path("sitemap.xml", sitemap, {"sitemaps": sitemaps}, name="django.contrib.sitemaps.views.sitemap",),

] 

It is also linked in my template usingn<link rel="icon" type="image/x-icon" href="{% static 'img/favicon.ico' %}">


Solution

  • Your question already has been answered here: How to get a favicon to show up in my django app?

    In short you can create a new view that redirect favicon.ico to /static/img/favicon.ico or you just put the favicon with HTML:

    <link rel="shortcut icon" type="image/png" href="{% static '/img/favicon.ico' %}"/>
    

    Displaying the favicon in Google takes time, looks like you already did everything, so you just need to wait a little bit and if it doesn't appear, check the thread I shared.