在 Django 中使用时不显示字体

Font is not displayed when I use it in Django

我将 .ttf 文件和 html 文件放在同一个文件夹中。在 html:

<style type="text/css">
@font-face { 
font-family: 'fontNameRegular'; 
src:local('fontName Regular'),
    local('fontName'), 
    url('Qarmic.ttf') format('truetype');
} 
</style>
<style>
#ads{font-family:'fontNameRegular';font-size:60px;font-weight:normal;}
#ads2{font-size:10px;font-weight:normal;}
</style>


<h2>System Font</h2>
 <div id="ads2">
     aaa
</div>

<h2>My Font</h2>
 <div id="ads">
     aaa
</div>

当我单独打开 html 文件时,它起作用了。 但是,当我使用django调用html文件时。

def view_preview(request):
    return render_to_response('preview.html')

字体不会显示。我真的需要帮助。谢谢。

您需要将字体文件放入您的 STATICFILES_DIRS 之一。 Django 不会提供与您的 HTML 文件相关的字体。相反,您需要移动它并使用:

{% load staticfiles %}

<style type="text/css">
    @font-face { 
    font-family: 'fontNameRegular'; 
    src:local('fontName Regular'),
        local('fontName'), 
        url({% static 'path/to/Qarmic.ttf' %}) format('truetype');
    } 
</style>

我在 Django 1.8 上遇到了同样的问题,但我的问题是 font-family 的名称不应该在 qutoes 中!!我真的不知道为什么!!!但是删除引号解决了我的问题,这是我在 html 文件中的工作方式:

{% load staticfiles %}

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

我使用此字体的方式是添加此 class,因此将此 class 添加到标签会导致其字体发生变化!希望对你有帮助!

<style type='text/css'>
    .myfont{ /* my font class */
        font-family:'Bnazanin';
    }
</style>