I'm trying to compile static with django-pipeline, but can't manage to serve static in dev mode. Since I'm not a Django developer, I may be wrong how Django serves static files itself. Here's my project structure:
As I'm using shared static, I've specified STATICFILES_DIRS
directive to allow Django dev server and collectstatic
command to find shared static:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
Here's my settings.py
:
# for production, should be served via nginx
STATIC_ROOT= os.path.join(BASE_DIR, 'public/static/')
# prefix for static app
STATIC_URL = '/static/'
# also django-pipeline config
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'pipeline.finders.PipelineFinder',
)
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
PIPELINE = {
'PIPELINE_ENABLED': True,
'COMPILERS': (
'pipeline.compilers.stylus.StylusCompiler',
),
'STYLESHEETS': {
'app': {
'source_filenames': (
'css/app.styl',
),
'output_filename': 'css/app.css',
},
},
}
In my template I've specified CSS-group:
{% load pipeline %}
{% stylesheet 'app' %}
</head>
As a result, such HTML is generated:
<link href="/static/css/app.css" rel="stylesheet" type="text/css" />
</head>
But /static/css/app.css
returns 404.
If I run collectstatic
, public/static/css/app.css
is built. But as I understand Django logic, it's used only for apache/nginx-based production serving and not in dev one. In dev mode static is served via internal Django server with some middleware django-pipeline hooks.
What I'm doing wrong? Any help is appreciated, thanks.
UPD: I've added + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
to global urlpatterns, however can't understand it's why it's recommended — STATIC_ROOT is used only for production with external proxy-server and not with Django itself. Or not?
For everyone, fighting with the same problem. Just remove 'PIPELINE_ENABLED': True,
away from django-pipeline config. After this, django-pipeline will re-compile static for each request in dev mode.
And before deployment, just run collectstatic
and set DEBUG=False
, as it's mentioned in docs. After this pipeline will stop re-compiling files for each request, load minified assets in templates and you'll be able to serve static from STATIC_ROOT
with any third-part proxy-server like NGINX.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With