Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

986
Views
How to use TailwindCSS with Django?

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?

When googling, I found a python package named django-tailwind but it did not really helped me in the process.

over 4 years ago · Santiago Trujillo
7 answers
Answer question

0

A bit late, but I'v recently wrote a blog post about this, focused on replacing the Gulp and Bootstrap in Django Cookiecutter with Webpack and TailwindCSS. This is the link: https://www.boringbackend.dev/using-tailwindcss-cookiecutter-django/

over 4 years ago · Santiago Trujillo Report

0

TL;DR

  1. Install TailwindCSS within your Django project, like any JS project with npm
  2. Use a live-reload server package with Django
  3. Add purgeCSS config before deploying

More detailed explanation

1 - The TailwindCSS build process

Create a new directory within your Django project, in which you'll install tailwindCSS like in any vanilla JS project setup:

cd your-django-folder; mkdir jstoolchain; cd jstoolchain
npm install tailwindcss postcss-cli autoprefixer
npx tailwind init
touch postcss.config.js

In this postcss.config.js file, add:

module.exports = {
    plugins: [
        require('tailwindcss'),
        require('autoprefixer')
    ]
}
mkdir css; touch css/tailwind.css

In this tailwind.css file, add at least this:

@tailwind base;
@tailwind components;
@tailwind utilities;

Now, add a script in your jstoolchain/package.json file to create the build process and specify the output file, such as:

{
  "scripts": {
    "build": "postcss css/tailwind.css -o ../your-django-folder/your-path-to-static/css/tailwind-output.css"
  }
}

Now, run;

npm run-script build

This should run without error, and tailwind-output.css should be now filled with thousands of lines. Now you can actually use tailwindCSS classes, by including the outputted css file into a Django template file along with Django's call to load the static files:

{% load static %}

<head>
  <link rel="stylesheet" href="{% static "css/tailwind-output.css" %}">
</head>

2 - Handling auto-reload locally

What's missing now to ease development, is to auto-reload the django development server when an HTML file is changed and saved. For this, I installed django-livereload-server.

Just follow setup instructions, this will work as expected out of the box, without any special configuration.

3 - The purgeCSS process

When you're ready to deploy, to ensure the CSS output file is not bloated with useless classes, go to jstoolchain/tailwind.config.js file, and add:

  purge: {
    enabled: true,
    content: ['../your-django-folder/path-to-your-templates/**/*.html'],
  },

Now, running build script should produce a much smaller CSS output, production-ready file.

EDIT: Tailwind 2.1 comes with an optional Just-In-Time compiler. The purge section will still be required, but you won't need to run a purge script before deploying, among many other advantages. Take a look here for more informations


Ideas to improve the workflow

  • The build script could be run automatically when the input tailwind files (css, js) are edited
  • (Only if you don't use just-in-time compiler) PurgeCSS could be run automatically when required, rather than adding it or removing it manually.
over 4 years ago · Santiago Trujillo Report

0

1. Go to your desired folder for installation. In my case:

 mkdir static/css/tailwind

 cd static/css/tailwind

2. Create package.json:

npm init -y

3. Install Tailwind via npm:

npm i tailwindcss

4. Create a css file and add code from official Tailwind documentation:

@tailwind base;
@tailwind components;
@tailwind utilities;

5. Open package.json and make this change to "scripts":

  "scripts": {
    "build:css": "tailwind build tw.css -o ../tailwind.css"
  },

6. Run the written script

npm run build:css

tw.css is the location of the file we created in 4th step. And ../tailwind.css is the location of the file we want the Tailwind css to be outputted. So, after running this command we will have a tailwind.css file with Tailwind base, components and utilities.

over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report

0

Found this awesome repository on github => https://github.com/timonweb/django-tailwind Can be initialized simply with python manage.py tailwind start

Features:

  • supports hot reloading
  • supports latest tailwind version

Install the django-tailwind package via pip:

python -m pip install django-tailwind

Alternatively, you can install the latest development version via:

python -m pip install git+https://github.com/timonweb/django-tailwind.git

Add 'tailwind' to INSTALLED_APPS in settings.py:

INSTALLED_APPS = [ 'tailwind', ]

Create a Tailwind CSS compatible Django app, I like to call it theme:

python manage.py tailwind init

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

INSTALLED_APPS = [ 'tailwind', 'theme' ]

Register the generated 'theme' app by adding the following line to settings.py file:

TAILWIND_APP_NAME = 'theme' Make sure that the INTERNAL_IPS list is present in the settings.py file and contains the 127.0.0.1 ip address:

INTERNAL_IPS = [ "127.0.0.1", ]

Install Tailwind CSS dependencies, by running the following command:

python manage.py tailwind install

The Django Tailwind comes with a simple base.html template located at your_tailwind_app_name/templates/base.html. You can always extend or delete it if you already have a layout.

If you are not using the base.html template that comes with Django Tailwind, add {% tailwind_css %} to the base.html template:

{% load tailwind_tags %} <head> {% tailwind_css %} </head>

The {% tailwind_css %} tag includes Tailwind’s stylesheet.

Let’s also add and configure django_browser_reload, which takes care of automatic page and css refreshes in the development mode. Add it to INSTALLED_APPS in settings.py:

INSTALLED_APPS = [ 'tailwind', 'theme', 'django_browser_reload' ]

Staying in settings.py, add the middleware:

MIDDLEWARE = [ "django_browser_reload.middleware.BrowserReloadMiddleware", ]

The middleware should be listed after any that encode the response, such as Django’s GZipMiddleware. The middleware automatically inserts the required script tag on HTML responses before when DEBUG is True.

Include django_browser_reload URL in your root url.py:

from django.urls import include, path

urlpatterns = [ path("__reload__/", include("django_browser_reload.urls")), ]

Finally, you should be able to use Tailwind CSS classes in HTML. Start the development server by running the following command in your terminal:

python manage.py tailwind start

over 4 years ago · Santiago Trujillo Report

0

Vimesh style is a full-featured tiny javascript library alternative to Tailwind CSS 2.0. Just add one line to your html page, it will take care the rest. https://github.com/vimeshjs/vimesh-style

over 4 years ago · Santiago Trujillo Report

0

Tailwind + Django starter

- Fully supports tailwind inplace html styling.
- And a main.scss file to add your custom styles.

Clone the project

https://github.com/MindMansion/DjangoTailwindStarter

OR

From your Django project directory

  mkdir theme
  cd theme
 
  npx degit https://github.com/MindMansion/DjangoTailwindStarter/theme
  npm install
  npm run build
  npm run watch

A global.css file should be ready for use in your static directory.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!