Search code examples
pythondjangoreactjsdjango-staticfilesstatic-files

Why isn't favicon shown up in production?


Favicon isn't shown up in the tab though Django's response code is 200. Static files are located in 'myproject/build' (and 'settings.py' in 'myproject/server', and app in 'myproject/blog'). Though HTML page with css and js files is served well. And 'favicon.ico' is located in the same directory as 'index.html' page

I tried to open a page by "localhost:8000' address in different browsers and I got the same thing: favicon isn't shown up in the tab. I also looked at "Sources" in developers options: there aren't any 'favicon.ico' file

settings.py :

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'build/static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

urls.py (in 'server/'):

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('blog.api.urls')),
    re_path('.*', TemplateView.as_view(template_name='index.html'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

index.html (in 'build/') :

<link rel="shortcut icon" href="/favicon.ico" />

Console:

[10/Jun/2019 09:57:29] "GET /favicon.ico HTTP/1.1" 200 2242

UPD: I ran command python manage.py collectstatic but I still get the same issue with favicon


Solution

  • your favicon located in static folder:

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