gpt4 book ai didi

python - Django 管道不加载 Assets

转载 作者:行者123 更新时间:2023-11-28 19:29:32 25 4
gpt4 key购买 nike

我关注了this tutorial制作示例项目。文件的结构是:

- mysite
- mysite
- __init__.py
- settings.py
- urls.py
- wsgi.py
- polls
- migrations
- templates
- polls.html
- static
- script.js
- style.css
- admin.py
- models.py
- tests.py
- urls.py
- views.py
- manage.py

一切正常,但问题是使用 Django-pipeline用于管理 Assets 。我已将我的项目配置为与以下代码相同,但它无法正确加载 Assets 。

settings.py

INSTALLED_APPS = (
.
.
'django.contrib.staticfiles',
'pipeline',
'polls',
)

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
PIPELINE_ENABLED = True
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
PIPELINE_CSS_COMPRESSOR = 'pipeline.compressors.cssmin.CSSMinCompressor'
PIPELINE_CSSMIN_BINARY = 'cssmin'
PIPELINE_JS_COMPRESSOR = 'pipeline.compressors.slimit.SlimItCompressor'

PIPELINE_CSS = {
'pollsX': {
'source_filenames': (
'style.css',
),
'output_filename': 'styles1.css',
'variant': 'datauri',
},
}
PIPELINE_JS = {
'pollsX': {
'source_filenames': (
'script.js',
),
'output_filename': 'scripts1.js',
}
}

polls.html

{% load compressed %}
{% compressed_css 'pollsX' %}

<div class='red-me'>
<h1> Hi! I'm a templ! </h1>
</div>

样式.css

.red-me {
color: red;
}

http://127.0.0.1/polls 生成的输出是

<link href="/static/styles1.css" rel="stylesheet" type="text/css" />

<div class='red-me'>
<h1> Hi! I'm a templ! </h1>
</div>

它无法在浏览器中加载 /static/styles1.css 文件。甚至,我测试了 ./manage.py collectstatic 但没有成功。我错过了什么吗?

Python-3.4 和 Django-1.7

最佳答案

Django 管道更新非常频繁,因此您的特定教程已经过时了。但我还是想回答你的问题,因为我刚刚花了几个小时用新管道解决了同样的问题,我想分享我的解决方案并希望它对某人有所帮助。

一切都适用于:

  • Django==1.9.1
  • django-pipeline==1.6.4

settings.py

INSTALLED_APPS = (
.
.
'django.contrib.staticfiles',
'pipeline',
'polls',
)

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')

STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'pipeline.finders.PipelineFinder',
)

STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'

PIPELINE = {
'CSS_COMPRESSOR': 'pipeline.compressors.cssmin.CSSMinCompressor',
'CSSMIN_BINARY': 'cssmin',
'JS_COMPRESSOR': 'pipeline.compressors.slimit.SlimItCompressor',
'STYLESHEETS': {
'pollsX': {
'source_filenames': (
'style.css',
),
'output_filename': 'css/styles1.css',
'variant': 'datauri',
},
},
'JAVASCRIPT': {
'pollsX': {
'source_filenames': (
'script.js',
),
'output_filename': 'js/scripts1.js',
},
}
}

polls.html

{% load pipeline %}
{% stylesheet 'pollsX' %}
{% javascript 'pollsX' %}

<div class='red-me'>
<h1> Hi! I'm a templ! </h1>
</div>

关于python - Django 管道不加载 Assets ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26262871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com