Django - New fonts?

前端 未结 2 1533
慢半拍i
慢半拍i 2020-12-14 02:08

How do I install new fonts with Django? There is no mention of this in the documentations.

I have my fonts installed in the static folder as such fonts/abc.ttf

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 02:53

    For the directory structure like so,

    -- static
     |--fonts
     | |--abc.ttf
     |
     |--css
       |-- main.css
    

    In the main.css, you should add.

    @font-face {
      font-family: 'abc';
      src: local('Abc'),
           url('../static/fonts/abc.ttf') format("truetype");
    }
    

    You can't use {% static 'filename' %} inside a css file, since it will not be rendered by the django templating engine.

    Also, if you want you can add the following in the section of base.html, and it will render a fully qualified path for static assets:

    
    

    Edit: Fixed the use of local and also removed the preference around location of style tag in html.

提交回复
热议问题