Adding Google Fonts to Flask

限于喜欢 提交于 2021-02-07 20:36:56

问题


So with flask I know that I can add CSS with

<link ... href="{{ url_for('static', filename='stylesheets/style.css') }}" />

but if I am adding a google font which usually in HTML looks like

<link href='http://fonts.googleapis.com/css?family=PT+Sans:400,700' rel='stylesheet' type='text/css'>

what do I need to edit/add for it to work with Flask?


回答1:


You can't use static here like you would normally for a link to a resource on the webserver. Your link is still essentially static but not related to anything you are serving. So you either put the Google font link directly in the HTML template, or as a variable expanding to the full link (which would be convenient if you aren't using the same header template everywhere and may change the font later).




回答2:


Add the google font link to your index.html or any page that you want like this:

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block styles %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename= 'mystyle.css') }}">
<link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>
{% endblock %}

Then, add the CSS font statement to your custom CSS file (mine is static/mysyle.css) :

body {
    font-family: 'Open Sans Condensed', sans-serif;`
}


来源:https://stackoverflow.com/questions/29375046/adding-google-fonts-to-flask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!