This guide will help you integrate TailwindCSS into your Django
project using the popular package django-tailwind.
Step 1: Install Required Packages
First, install django-tailwind and its dependencies:
pip install django-tailwind
pip install django-browser-reload
Step 2: Add to INSTALLED_APPS
Open your settings.py and add:
INSTALLED_APPS = [
...
'tailwind',
'theme',
'django_browser_reload',
]
Also add this middleware:
MIDDLEWARE = [
...
"django_browser_reload.middleware.BrowserReloadMiddleware",
]
Step 3: Create Tailwind App
Run this command:
python manage.py tailwind init theme
This creates a new Django app named theme that contains Tailwind
configuration.
Then navigate to the theme directory:
cd theme
Step 4: Install Node Dependencies
Inside the theme folder, run:
npm install
This installs Tailwind and all required frontend dependencies.
Step 5: Configure Tailwind
Open the tailwind.config.js and update the content section:
module.exports = {
content: [
"../../templates/**/*.{html,js}",
"../../**/templates/**/*.{html,js}",
],
theme: {
extend: {},
},
plugins: [],
};
Step 6: Start Tailwind Watcher
Now run Tailwind in dev mode:
python manage.py tailwind start
This will compile CSS automatically whenever you edit your template
files.
Step 7: Load Tailwind CSS in Template
In your base template:
{% load static %}
<link href="{% static 'theme/css/dist/styles.css' %}" rel="stylesheet">
Step 8: Enable Live Reload (Optional but Recommended)
Add to your urls.py:
from django.urls import path, include
urlpatterns = [
...
path("__reload__/", include("django_browser_reload.urls")),
]
And in your base template, just before </body>:
{% if debug %}
<script src="http://localhost:3000/browser-sync/browser-sync-client.js"></script>
{% endif %}
Done!
You have successfully integrated Django + TailwindCSS using
django-tailwind.
Now you can use Tailwind classes directly inside your Django templates
to build beautiful UI faster.
Happy coding!
Top comments (0)