The font is not showing when I use it in Django
I put the .ttf file and html file in the same folder. In 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>
When I open the html file separately, it works. However, when I use django to call the html file.
def view_preview(request):
return render_to_response('preview.html')
The font will not appear. I really need help. Thank.
You need to put the font file in one of yours STATICFILES_DIRS
. Django will not use the font relative to your HTML file. Rather, you will want to move it around and use:
{% 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>
source to share
I had the same problem with Django 1.8, but my problem was that the font-family name should not be in qutoes !! I really really don't know why !!! But removing the quotes solved my problem, this is how I did it and worked in the html file:
{% load staticfiles %}
<style>
@font-face {
font-family: Bnazanin;
src: url("{% static 'font/Bnazanin.ttf' %}" ) format("truetype");
}
</style>
I used this font in such a way that I add this class, so adding this class to the tag causes it to change the font! hope this helps you!
<style type='text/css'>
.myfont{ /* my font class */
font-family:'Bnazanin';
}
</style>
source to share