Django-Bower + Foundation 5 + SASS,如何配置? [英] Django-Bower + Foundation 5 + SASS, How to configure?

查看:675
本文介绍了Django-Bower + Foundation 5 + SASS,如何配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django-Bower测试Foundation 5的SASS实现。我对Bower的想法很新鲜,对于如何让此设置正常工作有点困惑。



我安装了django-bower,并配置为运行正常。在我添加基础到bower应用程序配置和运行 manage.py bower_install 后,我可以看到基础文件确实已正确安装。我也可以使用静态标签将JS加载到模板中没有问题,所以我觉得我大约在一半。



我的问题是关于如何实际使用安装了我的自定义SASS文件的基础文件,以及将这些SASS文件编译为CSS的最佳方式。我知道我应该能够包括基础到我的SASS与 @includefoundation但我失去了如何获得SASS文件请参见components / bower_components / foundation / sass中的基础文件以及如何设置编译将css放入正确的静态文件。



UPDATE:
PyCharm有一个选项来观看sass文件和编译它们,所以我现在有一个选项来编译文件,但当我试图导入基础我得到错误blog3.sass(行1:文件导入



为了参考,这里是我的文件结构:

 ├──blog3 
│└──__pycache__
├──组件
│└──bower_components
│├──foundation
││├──css
││├──js
│││├──基础
│││└──供应商
││└─ ─scss
││└──基础
││└──组件
│├──jquery
│└──modernizr
│├──特征 - 检测
│├──媒体
│└──测试
│├──caniuse_files
│├──js
││└──lib
│└──qunit
└──接口
├──迁移
│└──__pycache__
├──__pycache__
├──sass
└──模板
└──接口

这是我的settings.py



 
blog3项目的Django设置。

有关此文件的更多信息,请参阅
https://docs.djangoproject.com/en/dev/topics/settings/

有关完整列表的设置及其值,请参阅
https://docs.djangoproject.com/en/dev/ref/settings/


#在项目中构建路径像这样:os.path.join(BASE_DIR,...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__ file__))


#快速开始开发设置 - 不适合生产
#查看https://docs.djangoproject.com/en/dev/howto/deployment/checklist/

#安全警告:保持在生产秘密中使用的秘密密钥
SECRET_KEY ='...'

#安全警告:不要在生产中打开调试运行
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


#应用程序定义

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。 ,
'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。应用程序'


#数据库
#https://docs.djangoproject.com/en/dev/ref/settings/#databases

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

#国际化
#https://docs.djangoproject.com/en/dev/topics/i18n/

LANGUAGE_CODE ='en-us'

TIME_ZONE ='UTC'

USE_I18N = True

USE_L10N = True
b $ b USE_TZ = True


#静态文件(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



<
  • 不使用pycharm监视器。

  • django-compressor 来编译scss文件,包括指南针

  • django-bower

  • 这是一个使用django-compressor如何编译scss文件的示例:



    appName / static / scss / app.scss:

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

    / *其他内容* /

    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 =stylesheettype =text / x-scss/>
    {%endcompress%}

    < / head>

    希望这有助于你。



    strong>



    也许这是一个更好的配置使用@import没有亲戚路径。
    -I arg:

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


    $ b b

    现在app.scss:

      @importfoundation 
    @importcompass;



    使用PYCHARM WATCHER



    m欣赏pycharm监视器


    1. 安装django-bower

    2. 从pycharm首选项添加SCSS监视器

    3. 在编辑监视器时,在参数字段中设置:



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



    $ b b

    所以,在scss文件中:

      @importfoundation 
    @importcompass;


    I'm in the process of testing out a SASS implementation of Foundation 5 using Django-Bower. I'm new to the idea of Bower and am having a bit of confusion as to how to get this setup working properly.

    I have django-bower installed and configured to run properly. After I added foundation to the bower apps config and ran manage.py bower_install I can see that the foundation files have indeed been installed properly. I can also use the static tag to load the JS into a template without an issue, so I feel like I'm about halfway there.

    My issue is in regards to how to actually use the foundation files that bower installed with my custom SASS files, and the best way to compile those SASS files into CSS. I know that I'm supposed to be able to include foundation into my SASS with @include "foundation" but I'm lost as to how to get the SASS file to "see" the foundation files in components/bower_components/foundation/sass and how to set up a compile to put the css into the correct static file.

    UPDATE: PyCharm has an option to watch sass files and compile them, so I now have an option for compiling the files, but when I attempt to import foundation i get error blog3.sass (Line 1: File to import not found or unreadable: foundation.

    For reference, here's my file structure:

    ├── 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
    

    This is my 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',
    )
    

    解决方案

    SOLUTION WITHOUT PYCHARM WATCHER

    1. Not using pycharm watcher.
    2. django-compressor to compile scss files including compass.
    3. django-bower

    This is an example of "how to compile scss files" with django-compressor:

    appName/static/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>
    

    Hope this help you.

    EDIT

    Maybe this is a better config to use @import without relatives paths. -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),
        )
    

    Now app.scss:

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

    USING PYCHARM WATCHER

    Lately I'm appreciating pycharm watcher

    1. Install django-bower
    2. Add a SCSS Watcher from pycharm preferences
    3. In the edit of watcher, into 'Arguments' field I set:

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

    So, in the scss file:

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

    这篇关于Django-Bower + Foundation 5 + SASS,如何配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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