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

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

问题描述

如何使用 Django 安装新字体?文档中没有提到这一点.

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

我的字体安装在 static 文件夹中,例如 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");
}

不能在css文件中使用{% static 'filename' %},因为它不会被django模板引擎渲染.

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

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

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天全站免登陆