Django-新字体? [英] Django - New fonts?

查看:191
本文介绍了Django-新字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Django安装新字体?在文档中没有提及此内容。

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

我在静态文件夹中安装了我的字体,例如fonts / abc.ttf

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

例如,在模板中,如果这是CSS,我会这样链接它:

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">

除了这不是CSS,而且我没有找到有关执行此操作的任何资源。

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

我是否像这样在CSS文件中包含链接?

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' %})
}

任何帮助将不胜感激。

推荐答案

对于这样的目录结构,

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

main.css ,您应该添加。

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

不能使用 {% ,因为它不会由django模板引擎呈现。

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

也,如果您愿意,可以在 base.html < head> 部分中添加以下内容,它将呈现静态资产的完全限定路径:

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 :修复了 local ,并且还删除了html中样式标签位置周围的偏好。

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

这篇关于Django-新字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆