在DjangoAssets/Webassets中设置要编译的目录时出现问题

2024-04-26 20:59:05 发布

您现在位置:Python中文网/ 问答频道 /正文

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

from django_assets import Bundle, register

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

register('sass', sass)

现在我遇到了一个问题,它说Another bundle is already registered as "sass",但没有看到如何注销它。你知道吗

无论如何,我将register('sass', sass)更改为register('sass_all', sass)以克服错误。当我去编译的时候,它是在我的scripts目录中查找的,我保存了manage.py。在settings.py我加上:

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: 'directory'OSError: The environment has no "directory" configuredAttributeError: 'list' object has no attribute 'startswith'。实际上,我认为directory没有定义只是一个错误。你知道吗

通读environment documentation,鉴于我的技能水平,这是含糊不清的。在assets.py中,将Environment添加到import就是说Could not import in Environment。添加Environment.directory('static')会导致Environment is not defined。仅仅directory('static')也会导致directory is not definedenv = Environment()同样的事情。尝试将directory='/static'添加到sass = Bundle(...)中,只显示TypeError: got unexpected keyword argument 'directory'。你知道吗

不管怎样,花了几个小时,又卡住了。文档似乎指出directory设置应该放在assets.py而不是settings.py,而ASSETS_ROOT应该放在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

继续这个问题:Trouble adding Django-Assets / Webassets directory to look for assets.py files


Tags: pyimportregistersettingsenvironmentismainhtml
1条回答
网友
1楼 · 发布于 2024-04-26 20:59:05

要注意的要点:

# 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)。你知道吗

相关问题 更多 >