📜 ⬆️ ⬇️

We use bower in django projects with django-bower



Many are already tired of climbing a bunch of sites and downloading js libraries and plugins. Yes, and drag once downloaded, but the long rotten version is not very cool. These problems are solved by bower .

But with him, too, a lot needs to be done manually: follow the components each project, pull the bower install with your hands. After pip with requirements.txt to do it even somehow laziness.
')
Therefore, I present to you django-bower , which takes care of installing and updating bower packages according to the list of settings and transparent work with staticfiles .

Installation


To begin with, you must have bower installed, the instruction is on the official website .

You need to put the django-bower from django-bower :
 pip install django-bower 

Add application to INSTALLED_APPS in settings :
 'djangobower', 

Connect it to STATICFILES_FINDERS :
 'djangobower.finders.BowerFinder', 

And set the path to components - the place where the installed packages are stored. The path must be absolute and exist:
 BOWER_COMPONENTS_ROOT = '/PROJECT_ROOT/components/' 

You can see an example of settings in the demo project .

Using


To use the packages, list them in BOWER_INSTALLED_APPS in settings , for example:
 BOWER_INSTALLED_APPS = ( 'jquery#2', 'underscore', ) 

And install them with the bower_install team, you can later use it to update packages:
 ./manage.py bower_install 

Now you can connect the script in the template:
 {% load static %} <script type="text/javascript" src='{% static 'jquery/jquery.js' %}'></script> 

In production, bower_install needs to be done before collectstatic :
 ./manage.py bower_install ./manage.py collectstatic 

To get BOWER_INSTALLED_APPS with fixed package versions, use bower_freeze :
 ./manage.py bower_freeze 

Source: https://habr.com/ru/post/183094/


All Articles