How can I get a favicon to show up in my django app?

后端 未结 13 1590
盖世英雄少女心
盖世英雄少女心 2020-12-02 06:50

I just want to drop the favicon.ico in my staticfiles directory and then have it show up in my app.

How can I accomplish this?

I ha

13条回答
  •  孤城傲影
    2020-12-02 07:27

    In your settings.py add a root staticfiles directory:

       STATICFILES_DIRS = [
            os.path.join(BASE_DIR, 'static')
            ]
    

    Create /static/images/favicon.ico

    Add the favicon to your template(base.html):

    {% load static %}
    
    

    And create a url redirect in urls.py because browsers look for a favicon in /favicon.ico

    from django.contrib.staticfiles.storage import staticfiles_storage
    from django.views.generic.base import RedirectView
    
    urlpatterns = [
        ...
        path('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url('images/favicon.ico')))
    ]
    

提交回复
热议问题