While starting to use Sass / Compass with Django couldn't be much easier regardless of platform, it has taken a bit of searching around to find the best way to use CoffeeScript with Django on a Windows development box.
相关问题
- Django __str__ returned non-string (type NoneType)
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
- UnicodeEncodeError with attach_file on EmailMessag
相关文章
- 如何让cmd.exe 执行 UNICODE 文本格式的批处理?
- 怎么把Windows开机按钮通过修改注册表指向我自己的程序
- Warning : HTML 1300 Navigation occured?
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- Bundling the Windows Mono runtime with an applicat
- Django/Heroku: FATAL: too many connections for rol
This looks promising to me: http://pypi.python.org/pypi/django-coffeescript/
Node support on Windows has greatly improved since I posted my original answer (which I will leave for historical purposes), so now it's much easier to get this working.
Download and install Node using the Windows installer. You get
node
andnpm
commands added to your Windows PATH automatically (available in cmd.exe).Install CoffeeScript:
npm install -g coffee-script
. Then just to test, using cmd.exe...Install django-compressor:
pip install django-compressor
.Add to your settings.py so django-compressor will precompile your CoffeeScript.
Profit! Now use *.coffee files or inline CoffeeScript in Django templates and have it automatically compiled to javascript and combined with your other scripts into a single compressed file.
Example (taken from django-compressor docs):
Original answer (obsolete):
The goal is to be able to write CoffeeScript right inside Django templates and have it get automatically converted to Javascript (along with .coffee files). django-compressor has a precompiler that does this, prior to the file compression it's known best for.
Of course the issue is you want to use Windows (what's wrong with you?), and the precompiler assumes you have a typical Linux installation of node.js and coffee-script, able to invoke 'coffee' from the command line with all its standard options. To get the same functionality Windows (without resorting to cygwin), you just have to make a little .bat file:
Grab the latest Windows binary of node
Add the path containing node.exe to PATH in Windows system environment variables
Pick one of:
Given that npm is not available for Windows, you can use ryppi, a minimal Python node package manager, to install the coffee-script package. Put ryppi.py in your Python scripts folder.
Just download coffee-script from the main site
Add the path\to\coffeescript\bin (containing 'cake' and 'coffee') to your PATH in Windows system environment variables
Make a batch file so you can use 'coffee' from the command line (credit for this) by creating a coffee.bat file in path\to\coffeescript\bin folder above, with this as its contents:
Without this you have to do 'node \path\to\bin\coffee' instead of just 'coffee'.
Try reopening cmd.exe and type...
Now you're using the real coffee-script program on node.
Setup the django-compressor precompiler to use coffee.bat:
I put that in my local_settings.py file. Just leave off the .bat as usual in the settings file used by your Linux production server or development box. Windows wasn't happy without the .bat.
Profit!
Now you can use inline CoffeeScript in your Django templates, and have it automatically compiled to javascript and combined with all your other scripts into a single compressed .js file. I'll leave details of using django-compressor to it's documentation.
Django Pipeline (Django >= 1.5) supports CoffeeScript compilation, as well as loads of other stuff (e.g. LESS, SASS, JS/CSS minification, etc). Make sure you have CoffeeScript installed, then
pip install django-pipeline
, add 'pipeline' to your INSTALLED_APPS and then create the following config entry:Then you can set up files to compile as per the linked docs - basically just source file(s), destination file and a name. You can refer to the compressed files by this name in templates likes this:
You can use one of these CoffeeScript compilers.
Some of them support file system watching, like the official node package. So you can start a console and do
and all the coffeescript files in src will be automatically recompiled when they change. You don't need any special integration with django, although it might be nice.
I find the delay that compiling via compressor adds to be too much. So I compile on the client side instead, and check in the js files. Instant, and very convenient if you start watching files when the runserver command is run:
https://gist.github.com/EmilStenstrom/4761479