难以在Django-Assets/Webassets中进行编译的目录 [英] Trouble setting-up directories to compile from in Django-Assets / Webassets

查看:40
本文介绍了难以在Django-Assets/Webassets中进行编译的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前的 assets.py 文件:

from django_assets import Bundle, register

sass = Bundle(
    'build/main.sass',
    filters='sass',
    output='css/main.css'
)

register('sass', sass)

现在我遇到一个问题,说另一个捆绑包已经注册为"sass" ,但是看不到如何注销它.

Now I run into an issue with it saying Another bundle is already registered as "sass", but not seeing how to unregister it.

无论如何,我将 register('sass',sass)更改为 register('sass_all',sass),以克服错误.当我进行编译时,它会在我的 scripts 目录中查找,该目录中存放着 manage.py .在 settings.py 中,我添加:

At any rate, I change register('sass', sass) to register('sass_all', sass) to get past the error. When I go to compile it is looking in my scripts directory where I keep manage.py. In settings.py I add:

ASSETS_ROOT = [
  'static',
]

只是查看了不存在的 scripts/static .

尝试:

# This is already in settings.py
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

# Added new line
ASSETS_ROOT = [
    STATICFILES_DIRS,
]

它会产生几个错误: KeyError:目录" OSError:环境未配置目录" ,并且 AttributeError:"list"对象没有属性"startswith" .我认为,确实没有定义目录只是一个错误.

It generates a couple errors: KeyError: 'directory', OSError: The environment has no "directory" configured, and AttributeError: 'list' object has no attribute 'startswith'. Really it is just one error that directory isn't defined, I think.

通读环境文档,鉴于我的技能水平,该文档很模糊.在 assets.py 中,向 import 添加 Environment 只是说无法在环境中导入.添加 Environment.directory('static')会导致 Environment未定义.只是 directory('static')也导致未定义 directory . env = Environment()同样.尝试将 directory ='/static'添加到 sass = Bundle(...)中,它只表示 TypeError:得到了意外的关键字参数'directory'.

Read through environment documentation which is vague given my skill level. In assets.py adding Environment to import just says Could not import in Environment. Adding Environment.directory('static') results in Environment is not defined. Just directory('static') also results in directory is not defined. env = Environment() same thing. Tried adding directory='/static' to sass = Bundle(...) which just says TypeError: got unexpected keyword argument 'directory'.

无论如何,花了几个小时然后再次卡住.文档似乎表明 directory 设置应该放在 assets.py 中,而不是 settings.py 中,而 ASSETS_ROOT 应该是进入 settings.py .

Anyway, spent a few hours on it and stuck again. Documentation seems to indicate directory settings should go in assets.py and not settings.py, whereas ASSETS_ROOT should be going in settings.py.

再次感谢!

~/portal-client

project_dir
    apps
        account
            templates
                account
                    login.html
            forms.py
            urls.py
            views.py
        home
            templates
                home
                    home.html
            urls.py
            views.py
        results
    assets.py
    settings.py
    urls.py
scripts
    manage.py
static
    build
        main.js
        main.sass
    css
        app.css 
        main.css
    js
        app.js
        main.js
    media
templates
    base.html
    footer.html
    title.html

此问题的继续:无法添加Django-Assets/Webassets目录以查找assets.py文件

推荐答案

要注意的一点:

# This is already in settings.py
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
# After this line STATICFILES_DIRS is `[ 'BASE_DIR/static' ]`

# Added new line
ASSETS_ROOT = [
    STATICFILES_DIRS,
]
# After this line, ASSETS_ROOT is `[ [ 'BASE_DIR/static' ] ]`
# i.e., an array of arrays 
# I think you actually wanted:
ASSETS_ROOT = STATICFILES_DIRS[0] 
# - or more explicitly -
ASSETS_ROOT = os.path.normpath(os.path.join(BASE_DIR, 'static'))

这表示其中许多问题似乎是由相当不标准的django结构引起的(即,您的 manage.py 位于 scripts 目录中而不是 BASE_DIR 中).

That said many of these issues look like they are being caused by a fairly non-standard django structure (i.e., that your manage.py is in the scripts directory rather than in BASE_DIR).

这篇关于难以在Django-Assets/Webassets中进行编译的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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