简体   繁体   English

使用 Amazon s3 在 Django 中呈现的自定义字体

[英]Custom font to be rendered in Django using Amazon s3

I tried to read the documentation and still could not figure out how to render custom fonts in Django through s3 buckets.我试图阅读文档,但仍然无法弄清楚如何通过 s3 存储桶在 Django 中呈现自定义字体。

styles.css

@font-face {
  font-family: 'ProximaNova';
  src: url('../static/fonts/ProximaNova-Regular.otf');
  font-style: normal;
  font-weight: 400; 
}

settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'assets')
]

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

CRISPY_TEMPLATE_PACK = 'bootstrap4'

#s3 buckets config


AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME')
# AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None
AWS_S3_REGION_NAME = 'ap-south-1'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

Cors Configuration

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>http://localhost:8000</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
    <ExposeHeader>ETag</ExposeHeader>
    <ExposeHeader>x-amz-meta-custom-header</ExposeHeader>
</CORSRule>
</CORSConfiguration>

The code works fine in local environment.该代码在本地环境中运行良好。 Do I need AWS lambda for this or the absolute path defined in styles.css src: url('../static/fonts/ProximaNova-Regular.otf');我是否需要为此使用 AWS lambda 或src: url('../static/fonts/ProximaNova-Regular.otf');定义的绝对路径src: url('../static/fonts/ProximaNova-Regular.otf'); is incorrect?是不正确的?

Any help will be much appreciated..Thanks!任何帮助将不胜感激..谢谢!

@font-face {
  font-family: 'ProximaNova';
  src: url('/static/fonts/ProximaNova-Regular.otf');
  font-style: normal;
  font-weight: 400; 
}

Replace "../" with "/".用。。。来代替 ”/”。 So your url will be readed according to the root of your host.因此,您的 url 将根据主机的根目录读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM