簡體   English   中英

Django-Bower + Foundation 5 + SASS,如何配置?

[英]Django-Bower + Foundation 5 + SASS, How to configure?

我正在使用Django-Bower測試基礎5的SASS實現。 我對Bower的想法不熟悉,並且對於如何使這個設置正常工作有點困惑。

我安裝了django-bower並配置為正常運行。 在我為bower apps config添加基礎並運行manage.py bower_install我可以看到基礎文件確實已經正確安裝。 我也可以使用靜態標記將JS加載到模板中而沒有問題,所以我覺得我差不多就在那里。

我的問題是如何實際使用我自定義SASS文件安裝的基礎文件,以及將這些SASS文件編譯成CSS的最佳方法。 我知道我應該能夠通過@include "foundation"將基礎包含到我的SASS中但是我迷失了如何讓SASS文件“看到”組件/ bower_components / foundation / sass中的基礎文件以及如何設置編譯以將css放入正確的靜態文件中。

更新:PyCharm有一個選項來觀看sass文件並編譯它們,所以我現在有一個編譯文件的選項,但是當我嘗試導入基礎時我得到error blog3.sass (Line 1: File to import not found or unreadable: foundation.

作為參考,這是我的文件結構:

├── blog3
│   └── __pycache__
├── components
│   └── bower_components
│       ├── foundation
│       │   ├── css
│       │   ├── js
│       │   │   ├── foundation
│       │   │   └── vendor
│       │   └── scss
│       │       └── foundation
│       │           └── components
│       ├── jquery
│       └── modernizr
│           ├── feature-detects
│           ├── media
│           └── test
│               ├── caniuse_files
│               ├── js
│               │   └── lib
│               └── qunit
└── interface
    ├── migrations
    │   └── __pycache__
    ├── __pycache__
    ├── sass
    └── templates
        └── interface

這是我的settings.py

"""
Django settings for blog3 project.

For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '...'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'annoying',
    'django_extensions',
    'djangobower',
    'interface',

)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'blog3.urls'

WSGI_APPLICATION = 'blog3.wsgi.application'


# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True  


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/dev/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_FINDERS = (
    'djangobower.finders.BowerFinder',
)

BOWER_COMPONENTS_ROOT = os.path.join(BASE_DIR, "components")
BOWER_INSTALLED_APPS = (
    'jquery',
    'foundation',
)

解決方案沒有PYCHARM WATCHER

  1. 不使用pycharm觀察者。
  2. django-compressor編譯scss文件,包括指南針
  3. Django的亭子

這是使用django-compressor的“如何編譯scss文件”的示例:

APPNAME /靜態/ SCSS / app.scss:

@import "../../../components/bower_components/foundation/scss/foundation";
@import "compass";

/* other stuff*/

settings.py:

STATICFILES_FINDERS = (
    .....
    'compressor.finders.CompressorFinder',

)

COMPRESS_PRECOMPILERS = (
    ('text/x-sass', 'sass --compass "{infile}" "{outfile}"'),
    ('text/x-scss', 'sass --scss --compass "{infile}" "{outfile}"'),
)

COMPRESS_URL = '/static/'

template.html:

{% load static %}
{% load compress %}

<head>
    {% compress css %}
        <link href="{% static 'scss/app.scss' %}" media="screen, projection" rel="stylesheet" type="text/x-scss" />
    {% endcompress %}

</head>

希望這對你有所幫助。

編輯

也許這是一個更好的配置,使用沒有親戚路徑的@import。 -I arg:

PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
BOWER_COMPONENTS_ROOT = os.path.join(PROJECT_PATH, "../components/")
COMPRESS_PRECOMPILERS = (
        ('text/x-sass', 'sass --compass "{infile}" "{outfile}"'),
        ('text/x-scss', 'sass --scss --compass -I "%s/bower_components/foundation/scss" "{infile}" "{outfile}"' % BOWER_COMPONENTS_ROOT),
    )

現在app.scss:

@import "foundation";
@import "compass";

使用PYCHARM WATCHER

最近我很欣賞pycharm觀察者

  1. 安裝django-bower
  2. 從pycharm首選項添加SCSS Watcher
  3. 在觀察者的編輯中,進入'參數'字段我設置:

    --compass -I“/ $ ProjectFileDir $ / components / bower_components / foundation / scss”--no-cache --update $ FileName $:$ FileNameWithoutExtension $ .css

所以,在scss文件中:

@import "foundation";
@import "compass";

包:

  1. Django的管道
  2. Django的亭子

如何用django-pipeline編譯:

application.scss:

@import "../../../components/bower_components/foundation/scss/foundation";

settings.py:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'pipeline',
    'djangobower',
)

BOWER_COMPONENTS_ROOT = os.path.join(BASE_DIR, 'components')

STATICFILES_FINDERS = (
    .....
    'djangobower.finders.BowerFinder',  # just for bower components

)

PIPELINE_CSS = {
    'application': {
        'source_filenames': (
            'css/application.scss',
        ),
        'output_filename': 'css/application.css',
        'extra_context': {
            'media': 'screen,projection',
        },
    },
}

PIPELINE_COMPILERS = (
    'pipeline.compilers.sass.SASSCompiler',
)

然后在模板中:

{% load compressed %}
{% compressed_css 'application' %}

這將在developpemnt上編譯,而collectstatic將編譯和壓縮

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM