简体   繁体   中英

How did Django find Bootstrap files under this folder “/usr/local/lib/python3.6/site-packages”?

I recently started to work on a Django Bootstrap project and it started to become confusing. I installed the Django and created a project. Normally, it works as it should be and I'm able to display the app from my browser.

However, I wanted to personalise the template and decided to go with bootstrap4, which is a really cool framework. I installed bootstrap with this command "pip install django-bootstrap-static". I'm aware that I should include my static files in to the STATIC folder and specify it on to the settings.py, which I did, but I couldn't find the bootstrap4 files on the machine at the first place.

The thing is, I included the library references on the the html file like this;

<link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.min.css' %}">
<script src="{% static 'bootstrap/js/jquery.min.js' %}"></script>
<script src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script>

and suddenly, it started to work with bootstrap, which means my bootstrap elements are there!

But as I said, I didn't specify the bootstrap files, only thing I specified is my STATIC folder under the app. The bootstrap files which I found later on is under this folder "/usr/local/lib/python3.6/site-packages".

I want to include SASS to personalise even more my site but I didn't understand how Django works on that situation. Can you please explain to me this ?

Thanks

Let's start by taking a look at how Django's STATICFILES_FINDERS works:

The default will find files stored in the STATICFILES_DIRS setting (using django.contrib.staticfiles.finders.FileSystemFinder) and in a static subdirectory of each app (using django.contrib.staticfiles.finders.AppDirectoriesFinder). If multiple files with the same name are present, the first file that is found will be used.

Note the line that it searches the static subdirectory of each app .

Now let's take a look at the folder structure of django-bootstrap-static , we see two main folders bootstrap and fontawesome . And if you look inside, they only contain __init__.py and a folder named static .

Basically, both bootstrap and fontawesome can be seen as Django apps (ie similar to apps you create with python manage.py startapp ). By including bootstrap and fontawesome into your INSTALLED_APPS , you've included them as apps in your own Django project and Django's STATICFILES_FINDERS will look into these two apps too to look for folders named static . So in this case it's able to find these static files and include it when you run python manage.py collectstatic .

EDIT

I forgot to answer the part about SASS. To do so, I would probably use a library like django-sass-processor and also create my own app called bootstrap using the python manage.py startapp bootstrap and create a folder called static inside. Within the static folder I would create my SASS folder structure and do my necessary overrides and personalisations.

Bonus: A nice way to architecture your SASS project can be found here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM