Use Django with Bower and Pipeline
Intro
Since I decided to focus on python and JS, I started to dig into these two babies.
I use django as my web framework in python, so how to build a website easier is what I most concerned. I used yeoman for a while, pretty amazing and convenient, especially the bower and compressor, compiler.
So I did some search and study, trying to integrate the bower and compressor, compiler into django since I am getting used to the yeoman workflow.
How to do that
Python is great for extensions. It has so many modules you can find and build for it. So in order to integrate the bower and compressor, compiler into django, we need two modules:
django-bower
Just like other modules, you can install django-bower easily through pip
:
1 | pip install django-bower |
After you installed , you need add it into your INSTALLED_APPS
in your project settings with the name is ‘djangobower’. And If you don’t want to add the component file path by youself, you can use djangobower.finders.BowerFinder
to do that for you. Just add it into your INSTALLED_FINDERS
.
You can also set the BOWER_COMPONENTS_ROOT
to put all your packages into one place. And if you want, you can set the path to bower manually: BOWER_PATH=path_to_bower
So after these, how to use it?
Easy, you can manage your packages in your project settings with BOWER_INSTALLED_APPS=('jquery','bootstrap#4.0.0-alpha',)
, and put all your packages names into it. Just similar to what you did in bower.json
.
And for installing all the packages, you just need to run python manage.py bower_install -- --allow-root
(you can get rid of the -- --allow-root
if you don’t receive the sudo error)
And it will install all the packages you have listed in your settings.
The last step and most important step, you need to collect all your static files into your static folder. Just run:python manage.py collectstatic
.
Now you can just put
1 | {% raw %} |
in the top of your template files and use
1 | {% raw %} |
anywhere you want.
That’s it. Pretty much all about django-bower
.
django-pipeline
Now we got bower, so next step we need to get the compressor and compiler.
I use coffee-script as my pre-processor for JS. It’s easy and super cool!!! Strongly recommended!
Bower is a pretty cool package manager, but it has nothing to do with your own scripts or stylesheets or html files. So in order to compress the files to minimize the size of files and to use coffee-script, sass in django. You can install django-pipeline
.
Just like django-bower
, you can also install django-pipeline
with pip:
1 | pip install django-pipeline |
Same as django-bower
, you need put some essentials into your project setting file.
1 | INSTALLED_APPS = ( |
After finish the configuration, we can use it. In my case, I create a customize
folder to store all the customized styles and scripts. So i put this folder into my STATICFILES_DIRS
and create a simple group in PIPELINE_CSS
.
After that, just run the python manage.py collectstatic
. All your file will be copying and moving to your STATIC_ROOT
.
Same with django-bower
, you can set the path to sass
or coffee-script
manually if you want (like PIPEPLINE_SASS_BINARY=''
).
In your template, you can either load the file like what you did in django-bower
(or default way, precisely), or you can load it using pipeline
:
put
1 | {% raw %} |
into the top of your template files, and use
1 | {% raw %} |
to import the stylesheet, same for javascript with javascript
keyword.
All done.
Enjoy it
Now you have bower, coffee-script, sass in your django. Enjoy the modern web coding style!!
Thanks & Best!