How to use TailwindCSS with Django?

后端 未结 3 1432
独厮守ぢ
独厮守ぢ 2021-01-30 15:05

How to use all features of TailwindCSS in a Django project (not only the CDN), including a clean workflow with auto-reloading, and purgeCSS step to be production-ready?

W

3条回答
  •  爱一瞬间的悲伤
    2021-01-30 15:41

    Django-Tailwind CSS is a very good package and it works well for me. Follow the docs properly and you will be fine.

    Before you begin, make sure you have npm properly installed on your system

    Quick start

    1. Install the python package django-tailwind from pip

    pip install django-tailwind

    Alternatively, you can download or clone this repo and run pip install -e ..

    1. Add tailwind to INSTALLED_APPS in settings.py

    2. Create a tailwind-compatible Django-app, I like to call it theme:

    python manage.py tailwind init theme

    1. Add your newly created theme app to INSTALLED_APPS in settings.py

    2. In settings.py, register tailwind app by adding the following string:

    TAILWIND_APP_NAME = 'theme'

    1. Run a command to install all necessary dependencies for tailwind css:

    python manage.py tailwind install

    1. Now, go and start tailwind in dev mode:

    python manage.py tailwind start

    1. Django Tailwind comes with a simple base.html template that can be found under yourtailwindappname/templates/base.html. You can always extend it or delete it if you have own layout.

    2. If you're not using base.html template provided with Django Tailwind, add styles.min.css to your own base.html template file:

    You should now be able to use Tailwind CSS classes in your html.

    To build a production version of CSS run:

    python manage.py tailwind build


    For the live reload, this handles it: python manage.py tailwind start

    For the build process, this handles it: python manage.py tailwind build

    For the PurgeCSS process, see simple sample in the docs

    For NPM path configuration error (esp. on windows), see docs

提交回复
热议问题