如何在python-flask中添加自定义字体?

时间:2015-09-15 09:37:21

标签: python flask

我尝试使用@fontface css样式,但字体不会被渲染。 有没有其他方法可以使用python / flask ??

<!DOCTYPE html>
<html>
<style type="text/css">
@font-face {
font-family: trial;
src: url_for('CENTRALESANSCND-BOLD.otf') ;
font-weight:bold; }

</style>
<body>

<p style="font-family:trial; font-weight: bold"> Hello </p>

</body>
</html>

以上是我的HTML模板。 不幸的是,当我使用Flask渲染它时,它并没有反映出字体。 以下是我的.py代码:

from flask import Flask, render_template

app=Flask(__name__)
app.secret_key='1234'

@app.route('/', methods=['post', 'get'])
def output():
    c=2+3
    print c

    return render_template('f.html', c=c)

if __name__=='__main__':
    app.run(port=9876)

3 个答案:

答案 0 :(得分:4)

非常感谢您的帮助..所有建议的组合终于奏效了。

在此处发布解决方案:

CSS文件:

@font-face{
font-family: trial;
src: url('CENTRALESANSCND-BOLD.otf');
font-weight: bold;}

body {font-family: georgia; color:green}
h1 {font-family:trial; color: pink}

HTML文件:

<!DOCTYPE html>
<html>

<link type="text/css" rel="stylesheet" href="{{url_for('static', filename='mystyle.css')}}"/> 
<body>

<p > Hello... </p>
<h1> welcome </h1>

</body>
</html>

答案 1 :(得分:1)

url_for接受一个函数名称,你需要用双花括号括起来......试试:

<style type="text/css">
    @font-face {
    font-family: trial;
    src: {{ url_for('static', filename='CENTRALESANSCND-BOLD.otf') }};
    font-weight:bold; }
</style>

答案 2 :(得分:1)

  <style>
    @font-face {
      font-family: FontName;
      src: url("{{ url_for('static', filename='FontFile.otf') }}");
    }
  </style>