【问题标题】:Unable to locate file while rendering tag 'sass_src' in template在模板中呈现标签“sass_src”时无法找到文件
【发布时间】:2020-09-18 04:28:08
【问题描述】:

试图将 sass 集成到我的 django3.1 项目中。我曾尝试使用其他方法(例如 Django-compressor)来实现 sass,但也无法让它在那里工作。因此,任何有关实现 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 并且管理 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 无法找到该文件? 感谢您的任何建议!

【问题讨论】:

    标签: django sass django-staticfiles


    【解决方案1】:

    我遇到了同样的问题,就我而言,我必须像这样配置 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 相关.. 或者上面的列表中缺少某些查找器?

    【讨论】:

      猜你喜欢
      • 2019-02-28
      • 2020-07-13
      • 2010-09-20
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 2019-04-01
      • 1970-01-01
      相关资源
      最近更新 更多