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

后端 未结 13 1528
盖世英雄少女心
盖世英雄少女心 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:40

    Best practices :

    Contrary to what you may think, the favicon can be of any size and of any image type. Follow this link for details.

    Not putting a link to your favicon can slow down the page load.

    In a django project, suppose the path to your favicon is :

    myapp/static/icons/favicon.png
    

    in your django templates (preferably in the base template), add this line to head of the page :

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

    Note :

    We suppose, the static settings are well configured in settings.py.

    0 讨论(0)
  • 2020-12-02 07:41

    Universal solution

    You can get the favicon showing up in Django the same way you can do in any other framework: just use pure HTML.

    Add the following code to the header of your HTML template.
    Better, to your base HTML template if the favicon is the same across your application.

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

    The previous code assumes:

    1. You have a folder named 'favicon' in your static folder
    2. The favicon file has the name 'favicon.png'
    3. You have properly set the setting variable STATIC_URL

    You can find useful information about file format support and how to use favicons in this article of Wikipedia https://en.wikipedia.org/wiki/Favicon.
    I can recommend use .png for universal browser compatibility.

    EDIT:
    As posted in one comment,
    "Don't forget to add {% load staticfiles %} in top of your template file!"

    0 讨论(0)
  • 2020-12-02 07:44

    The best solution is to override the Django base.html template. Make another base.html template under admin directory. Make an admin directory first if it does not exist. app/admin/base.html.

    Add {% block extrahead %} to the overriding template.

    {% extends 'admin/base.html' %}
    {% load staticfiles %}
    {% block javascripts %}
        {{ block.super }}
    <script type="text/javascript" src="{% static 'app/js/action.js' %}"></script>
    
    {% endblock %}
    
    {% block extrahead %}
        <link rel="shortcut icon" href="{% static 'app/img/favicon.ico'  %}" />
    {% endblock %}
    {% block stylesheets %}
    
        {{ block.super }}
    {% endblock %}
    
    0 讨论(0)
  • 2020-12-02 07:44

    I tried the following settings in django 2.1.1

    base.html

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

    settings.py

     STATIC_ROOT = os.path.join(BASE_DIR, 'static')
     STATIC_URL = '/static/'` <br>`.............
    

    Project directory structure

    view live here

    0 讨论(0)
  • 2020-12-02 07:45
            <link rel="shortcut icon" type="image/png" href="{% static 'favicon/sample.png' %}" />
    

    Also run: python manage.py collectstatic

    0 讨论(0)
  • 2020-12-02 07:50

    Just copy your favicon on: /yourappname/mainapp(ex:core)/static/mainapp(ex:core)/img

    Then go to your mainapp template(ex:base.html) and just copy this, after {% load static %} because you must load first the statics.

    <link href="{% static 'core/img/favi_x.png' %}" rel="shortcut icon" type="image/png" />
    
    0 讨论(0)
提交回复
热议问题