Django在css文件中的url()上寻找错误的路径 [英] Django looking for wrong path on url() in css files

查看:61
本文介绍了Django在css文件中的url()上寻找错误的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个使用Django并托管在Heroku中的个人博客.您可以在此处进行检查: https://generic-blog.herokuapp.com/

I am trying to set up a personal blog that uses Django and is hosted in Heroku. You can check it here: https://generic-blog.herokuapp.com/

我所有的静态文件都按预期的方式加载到本地计算机上,直到我决定将静态文件存储在AWS S3上.我将settings.py配置为使用S3,并做了 python manage.py collectstatic 将静态文件收集到我的s3存储桶中.静态文件正在按预期加载,但是在我的CSS文件中有url()及其相对路径:

All my static files were loading as intended on my local machine until I decided to store the static files on AWS S3. I configured my settings.py to use S3 and did python manage.py collectstatic to collect the static files to my s3 bucket. The static files are loading as intended, but on my css files there are url() with relative paths as this one:

url("../fonts/blog.woff") format("woff")

由于路径不正确,因此没有加载.浏览器的控制台给我以下错误:

Which is not loading, as the path is incorrect. My browser's console gives me the following error:

downloadable font: download failed (font-family: "blog" style:normal weight:400 stretch:100 src index:1): status=2147746065 source: https://personal-django-blogs.s3.amazonaws.com/fonts/blog.woff

Django尝试搜索字体的路径错误 https://personal-django-blogs.s3.amazonaws.com/fonts/blog.woff 必须为https://personal-django-blogs.s3-sa-east-1.amazonaws.com/static_root/vendor/font-awesome/fonts/fontawesome-webfont.woff

The path that Django tries to search for the font is wrong https://personal-django-blogs.s3.amazonaws.com/fonts/blog.woff would have to be https://personal-django-blogs.s3-sa-east-1.amazonaws.com/static_root/vendor/font-awesome/fonts/fontawesome-webfont.woff

我可以将每个相对路径替换为s3存储桶中文件的绝对路径,但这感觉不对.我认为问题出在settings.py,但我无法理解为什么在s3上工作时相对路径不再起作用.

I could just replace each relative path with the absolute path of the file in s3 bucket, but this feels wrong. I think the problem lies in settings.py, but I couldn't manage to understand why when working on s3 the relative paths don't work anymore.

这是我的settings.py相关配置:

Here's my settings.py relevant configurations:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    BASE_DIR / 'static',
]
# Path to venv
VENV_PATH = os.path.dirname(BASE_DIR)

# Path for collectstatic to store files
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')

# User uploaded files
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(VENV_PATH, 'media_root')

DEFAULT_FILE_STORAGE = 'blog.s3_storages.MediaStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'

推荐答案

CSS

CSS url() is not processed by Django static files but called directly from Browser

当您使用相对路径时,需要解析路径值.如果您有多个部署,或者只是将其设置为绝对存储桶路径,则需要对该CSS文件进行一些预处理,以将存储桶名称设置为URL.

As you are using relative path resolved path value is expected. You will need to have some preprocessing of this CSS file to set the bucket name to URL if you have multiple deployments of it or just set it to absolute bucket path

这篇关于Django在css文件中的url()上寻找错误的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆