django-css
django-css copied to clipboard
Fixes to media paths
Some issues with the media paths came up when using django-css with Django 1.3 (beta).
- Since STATIC_URL is to be used in static urls picking up MEDIA_URL from template context causes problem (if they're not the same). This fix picks up COMPRESS_URL instead which doesn't clash with either of MEDIA_URL or STATIC_URL (and you can use {% with MEDIA_URL as COMPRESS_URL %} ).
- A change so that the storage backend's base_url is used when composing urls.
I believe that MEDIA_URL is for user-uploaded media now, so probably django-css shouldn't use MEDIA_URL at all. The current docs on staticfiles say to use STATIC_URL in your templates.
Your fix doesn't work for me, I still get:
Caught UncompressableFileError while rendering: "site/css/reset.css" is not in COMPRESS_URL ("/static/") and can not be compressed
Exception Location: /Users/me/envs/me/src/django-css/compressor/__init__.py in get_filename, line 66
derek73, you just need to place your css in the static folder or am I mistaken?
which static folder? the css file could be in a static folder in any app if you're using AppDirectoriesFinder or any other place you define by writing a custom finder and adding it to STATICFILES_FINDERS. Need to use staticfiles finders to find the css and js files.
This works for me:
https://github.com/derek73/django-css/commit/90be97f49411ec2c02ac7c177fcc86da557dd036
Though, I had to set COMPRESS_URL = STATIC_URL in order to make it work. I also had to change my default storage to use the staticfiles storage so that staticfiles could serve the files when DEBUG=True. When DEFAULT_FILE_STORAGE is StaticFilesStorage, DefaultStorageFinder finds things in STATIC_ROOT.
Here's my relevant settings.
MEDIA_ROOT = os.path.join(ROOT_PATH, 'static','uploads')
MEDIA_URL = '/static/uploads/'
STATIC_ROOT = os.path.join(ROOT_PATH, 'static')
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + "grappelli/"
DEFAULT_FILE_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
"django.contrib.staticfiles.finders.DefaultStorageFinder",
)
COMPRESS_URL = STATIC_URL
COMPRESS_ROOT = STATIC_ROOT
COMPRESS_OUTPUT_DIR = 'site'
I had to put COMPRESS_OUTPUT_DIR inside my COMPRESS_ROOT because the image urls in the css were not being rewritten to reflect the location of the compressed css file. The above happens to work because I put my site-specific css/img/js in a "site" directory inside "static" in each of my apps.
The problem is at compressor/filters/css_default.py line 18.
if not filename or not filename.startswith(media_root):
return self.content
that test should also include a test for if this file is one that is being served by static files and if so, go ahead and do the url rewriting. Not sure I follow how this module works though, so not exactly sure what/where the test should be.
Sorry for being vague. I meant in a place under your STATIC_URL. But you're right that my commits doesn't make it work with the development server, it only covers some basic stuff for making it work in production. So it's great that you've made some progress on that.
I've had problems getting staticfiles working reliable with the dev server, so I mostly use runserver it with --nostatic (after running collectstatic and serving the static dir with 'django.views.static.serve')
@derek73: the way I solved it was to remove the check on if the filename started with media_root (all of mine do, after collectstatic has been run). Then, on line 20, I have:
self.media_path = filename.split(media_url)[1]
This is a little bit hacky: I need to ensure that I only have the string /static/
in the filename once, but it does work.
Needed https://github.com/dziegler/django-css/pull/33 to make your patch work properly.
Also running into these issues with 1.3, agree that we should probably just nix MEDIA_URL, at least in the 1.3 world.
One oddity is why does MEDIA_URL in the context take priority over COMPRESS_URL? Seems like if you explicitly define a COMPRESS_URL that should take precedence.
You might wanna take a look at https://github.com/jezdez/django_compressor/. It now has a precompiler filter with which you can add support for external commands (sass, coffescript etc). Seems to work well and have support for Django 1.3 / staticfiles.