[英]How can I load a static font file for use with PIL in Django?
I'm trying to load a static font file using both settings.STATIC_URL
and plain string. 我正在尝试同时使用settings.STATIC_URL
和纯字符串加载静态字体文件。 In my app I have a folder named /static. 在我的应用程序中,我有一个名为/ static的文件夹。 and my font's path is /static/fonts/kberry.ttf 而我的字体的路径是/static/fonts/kberry.ttf
font = ImageFont.truetype(path_to_static, 12)
What should path_to_static
be? path_to_static
应该是什么? I've literally tried everything and all I get is IOError
. 我已经尝试了一切,而我得到的只是IOError
。
“I've literally tried everything” “我确实尝试了一切”
Literally everything? 从字面上看,一切都好吗? Every possible combination of characters that will fit into memory? 每种可能适合记忆的字符组合? I'm not sure what you think we can do then :) 我不确定您认为我们接下来可以做什么:)
But just in case, I'm guessing path_to_static
needs to be the full local filesystem path to the font file (rather than a web URL where the file can be found, which is what STATIC_URL
would contain). 但是以防万一,我猜测path_to_static
需要是字体文件的完整本地文件系统路径(而不是可以在其中找到该文件的Web URL,这就是STATIC_URL
所包含的内容)。
In my settings file, I declare a SITE_ROOT
variable, like this: 在我的设置文件中,我声明了一个SITE_ROOT
变量,如下所示:
import os
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
That sets SITE_ROOT
to the full path of the Django project folder. 这SITE_ROOT
设置为Django项目文件夹的完整路径。
If your static folder containing the font file is in the root of your project folder, then you should be able to get its path like this: 如果包含字体文件的静态文件夹位于项目文件夹的根目录中,那么您应该能够获得如下所示的路径:
import os, settings
path_to_static = os.path.join(settings.SITE_ROOT, 'static/fonts/kberry.ttf')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.