Package: django-cdn
django-cdnHave you ever wondered why using JS frontend libraries where so easy in modern frameworks and not Django? Well, this is your library!Django-cdn is a Django library that simplifies HTML imports of JS and CSS code with a NPM like command.
This library simplifies boosting your web app's security by effortlessly implementing CSP (Content Security Policy), making it more restrictive and robust.This library uses the NPM registry and JSDelivr APIs.InstallationInstall the library with pip:pip install django-cdnAdddjango-cdnto yourINSTALLED_APPS:INSTALLED_APPS = [
    ...
    'django_jwt',
    ...
]CommandInstallWith this command you will be save the libraries that you are using in thecdn-package.json.
Thanks to this you will be able to track your versions. Dependencies will be automatically handled.pythonmanage.pynpminstalljquerybootstrap@5.2.3If the automatic entrypoints of your library are not the ones that you wanted you can ask the install command to make you choose the entrypoints:pythonmanage.pynpm--entryinstalljqueryOutput:jquery: Installing version 3.7.1
1: /dist/jquery.js
2: /dist/jquery.min.js <-- Default entrypoint
3: /dist/jquery.slim.js
4: /dist/jquery.slim.min.js
Choose the entrypoints indexes in order separated by commas (Ex: 4, 2, 3):UninstallYou can uninstall any library with:pythonmanage.pynpmuninstalljquerybootstrapOr all at once with:pythonmanage.pynpmuninstall@BuildThis is where the library gets good. This command will automatically download all the required files to your static folders.This is thought to be done for deployment and have the folder ignored in git.pythonmanage.pynpmbuildUnbuildYou can easily delete the previous build without uninstalling the librarypythonmanage.pynpmunbuildjqueryOr all at once:pythonmanage.pynpmunbuild@TemplateAdd this to the beggining of the template:{% load bundle_loader %}To add your entrypoints to your django templates you can use the following:{% load_imports %}You can also load single packages or entrypoints with the following:{% load_only_imports 'jquery, bootstrap.js' %}This will include only the js files of Bootstrap and full Jquery package.In order to do all except some packages/entrypoints you can use the following:{% load_imports_except 'bootstrap.css' %}This will include all installed packages except the css entrypoints of bootstrap.NOTE: Thebootstrap.cssis not the file name, it is the package name +.+js or css.SettingsAll settings are optional except if you don't have anySTATICFILES_DIRSin your project.CDN_BUILD_STATIC_PATHConfigure the static folder where you want to install your cdn libraries.
If you haveSTATICFILES_DIRSthis is not needed as the library will install it to the first folder.#  This path must be a static folder. The libraries are installed on "/static/lib/"CDN_BUILD_STATIC_PATH='some/path/inside/BASE_DIR'CDN_PACKAGES_INFO_PATHConfigure the path where the cdn-package.json will be saved. Saving this file in git is highly recommended.CDN_PACKAGES_INFO_PATH='some/path/inside/BASE_DIR'SecurityYou can use this library to improve your webapp security withdjango-csp.
If you build the libraries you can just set this onsettings.py:DEBUG=...CSP_SCRIPT_SRC=['self']CSP_STYLE_SRC=['self']ifDEBUG:CSP_SCRIPT_SRC.append('data.jsdelivr.com')CSP_STYLE_SRC.append('data.jsdelivr.com')This will restrict the browser to retrieve the scripts from your domain which will improve security for your clients.