Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

python - Django - New fonts?

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

For instance, in a template if this was a CSS I would link it as such:

    <link href="{% static 'fonts/abc.ttf' %}" rel="stylesheet" media="screen">

except this is not CSS, and I haven't found any resources on how to to do this.

Do I include the link in the CSS file like so?

  @font-face {
  font-family: 'abc';
  src: url({% static 'fonts/abc.ttf' %});
  src: local({% static 'fonts/abc.ttf' %})
}

Any help would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 <head> section of base.html, and it will render a fully qualified path for static assets:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...