Easy to install and offline Google fonts in Django projects
Project description
django-google-fonts
lets you use Google fonts in Django easily, by downloading, rewriting and caching the font and CSS files locally.
README.md
This means that you can have all the benefits of using Google Fonts, but with added privacy, security and speed for your users, because all the requests for the fonts will be on your domain and not hitting Google servers.
When the server restarts it will check if any fonts are missing locally and load them if they are. So there is no impact or performance considerations. After that initial download of the fonts, django-google-fonts
does not need to make any more requests to Google servers, working totally offline from the Google servers.
Installing
pip install django-google-fonts
Then add to your Django settings file:
INSTALLED_APPS = [
...
'django_google_fonts'
]
Using
Tell Django which fonts you'd like:
GOOGLE_FONTS = ["Kablammo", "Roboto"]
When Django starts it will grab the fonts from Google and store them in your STATICFILES_DIRS
directory. It will rewrite the CSS that Google Fonts provides, so all you have to do is load the font like normal. For example:
<link rel="stylesheet" href="{% static 'fonts/pathwayextreme.css' %}">
<style>
body {
font-family: 'Pathway Extreme';
}
</style>
There is also a font
tag that will return the raw CSS:
{% load google_fonts %}
{% font_css "Pathway Extreme" %}
Custom font weights are available by specifying the font weights in the URL. The easy way to do this is visit a font page, for example Robot and then selecting the weights and styles you'd like. Then click on Selected Families
and copy the font definition in.
For example Google will suggest embedding the font using this URL:
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,700;0,900;1,700&display=swap" rel="stylesheet">
Roboto with italic in weights 100, 700, 900. To use this in Django you would specify:
GOOGLE_FONTS = ["Roboto:ital,wght@0,100;0,700;1,700"]
And you would reference it in a stylesheet:
<link rel="stylesheet" href="{% static 'fonts/roboto:ital,wght@0,100;0,700;1,700.css' %}">
Optional settings
By default django-google-fonts
will store fonts in the first directory specified in STATICFILES_DIRS
. That might not be where you want, so you can set a GOOGLE_FONTS_DIR
setting if you'd like it be somewhere else:
GOOGLE_FONTS_DIR = BASE_DIR / "fonts"
STATICFILES_DIRS = [BASE_DIR / "static", BASE_DIR / "fonts"]
The CSS file contains the path to the font and django-google-fonts
tries to calculate what the path to the font should be by using the value of STATIC_URL
. If that's wrong and you need it be something else, you can set that value:
GOOGLE_FONTS_URL = "my-exotic-static/url/to-the-fonts"
Names
Google fonts normally have title cased names, with capitalized first names [^1]. For example Pathway Extreme
. Google normalises this too: pathwayextreme
and this is used in file names. So in the case of Pathway Extreme
Where | Name |
---|---|
Settings file | Pathway Extreme |
Font tag | Pathway Extreme |
Static tag | pathwayextreme |
[^1]: Font's like IBM Plex Sans
have more capital letters than just the first letter.
If you are unsure you can get at the fonts programatically, for example:
>>> from django.apps import apps
>>> for font in apps.get_app_config("django_google_fonts").fonts:
... print(font.name, font.slug, font.dest)
...
Kablammo kablammo /Users/a/c/examplefonts/static/fonts/kablammo
Roboto roboto /Users/a/c/examplefonts/static/fonts/roboto
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_google_fonts-0.0.3.tar.gz
.
File metadata
- Download URL: django_google_fonts-0.0.3.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0439c4d89919970b141258bd3be0a085cc538f4e9c53e54f40eb2953ec2e5d30 |
|
MD5 | 3844226af9b3c7860fc940cbf5747031 |
|
BLAKE2b-256 | ede9a0d5a8988e0450444f69f99c94a896da5c4a0f8405ab8ab863cc377ece44 |
File details
Details for the file django_google_fonts-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: django_google_fonts-0.0.3-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8f0b943107932d5fbb66da26f2088e0c6d89cde8b66ed66c3f58a6f40d54994 |
|
MD5 | a8231777497877233ba687d171d1c25c |
|
BLAKE2b-256 | db357096ffbfadbd9acf332d99de078a56a48d7632f5b8c56714aa29f3333b36 |