问题描述
试图将sass集成到我的django3.1项目中。我尝试用其他方法(例如Django-compressor)实现sass,但也无法使其正常工作。因此,对于实现无礼的最终目标的任何建议,我们将不胜感激。
遵循https://terencelucasyap.com/using-sass-django/上的步骤,我尝试实现Django-sass-processor,但是当我转到主页时,它给我错误Unable to locate file style.scss while rendering tag 'sass_src' in template app/index.html
Settings.py
INSTALLED_APPS = [
...
#SASS
'sass_processor',]
...
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder','django.contrib.staticfiles.finders.AppDirectoriesFinder','sass_processor.finders.CssFinder',]
STATIC_ROOT = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
#DJANGO SASS
SASS_PROCESSOR_ROOT = STATIC_ROOT
base.html
{% load sass_tags %}
{% load static %}
...
<link href="{% sass_src 'style.scss' %}" rel="stylesheet" type="text/css" />
该网站似乎可以在管理页面上正常工作。 我运行了collectstatic,并且admin CSS与style.scss放在同一文件夹中,所以我认为STATIC_ROOT不会出现问题,但是我不确定错误发生在哪里。
树
../ROOT/
├── db.sqlite3
├── manage.py
├── README.md
├── requirements.txt
├── static
│ ├── admin
│ └── style.scss
├── PROJECT
│ ├── asgi.py
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── APP
├── admin.py
├── apps.py
├── forms.py
├── __init__.py
├── migrations
├── models.py
├── __pycache__
├── templates
│ └── APP
│ ├── base.html
│ ├── cityFrom.html
│ └── index.html
├── tests.py
├── urls.py
└── views.py
我缺少什么导致Django无法找到该文件? 感谢您提供的所有建议!
解决方法
我遇到了同样的问题,就我而言,我必须配置 STATICFILES_DIRS 设置,例如:
STATICFILES_DIRS = [ BASE_DIR / 'static' ]
注意 在这种情况下不需要 STATIC_ROOT 配置
django-sass-processor 使用查找器查找文件。看这里:
https://github.com/jrief/django-sass-processor/blob/master/sass_processor/storage.py#L26
模块的文档(如您所述)建议像这样配置它们:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder','django.contrib.staticfiles.finders.AppDirectoriesFinder','sass_processor.finders.CssFinder',]
如果没有 STATICFILES_DIRS,FileSystemFinder 将找不到任何东西。
问题可能与 django 3.x 相关.. 或者上面的列表中可能缺少某些查找器?