Django Tailwind
The django-tailwind library integrates the Tailwind CSS framework seamlessly into Django projects. It provides management commands to install, build, and serve Tailwind CSS, streamlining frontend development within a Django context. It is actively maintained with frequent releases, currently at version 4.4.2.
Common errors
-
command 'npm' not found
cause Node.js or npm is not installed, or not in your system's PATH.fixInstall Node.js and npm from their official website (nodejs.org) for your operating system. Verify installation with `node -v` and `npm -v`. -
Error: Cannot find module 'tailwindcss'
cause Tailwind CSS itself has not been correctly installed within the project's Tailwind app directory by npm.fixEnsure `TAILWIND_APP_NAME` is set in `settings.py` and then run `python manage.py tailwind install` to install `tailwindcss` and other npm dependencies into your project. -
Unknown command: 'tailwind'
cause `django_tailwind` is not included in your `INSTALLED_APPS` in `settings.py`.fixAdd `'django_tailwind'` to your `INSTALLED_APPS` list in `settings.py`. -
Staticfiles manifest entry for 'css/tailwind.css' not found
cause The Tailwind CSS file was not generated or collected into your static files during production deployment.fixEnsure you run `python manage.py tailwind build` to compile the CSS for production, and then `python manage.py collectstatic` to gather all static files, including the generated Tailwind CSS.
Warnings
- breaking Version 4.0 introduced several breaking changes including renaming `TAILWIND_CSS_PATH` to `TAILWIND_INPUT_CSS_PATH`, removing `TAILWIND_POSTCSS_OPTIONS` and `TAILWIND_NPM_PATH`, and requiring `TAILWIND_APP_NAME` to be set before `tailwind install`.
- gotcha django-tailwind requires Node.js and npm to be installed on your system for the Tailwind CLI to function correctly. These are not Python dependencies and must be installed separately.
- gotcha The `TAILWIND_APP_NAME` setting must be defined in your `settings.py` BEFORE running `python manage.py tailwind install`. Failure to do so can lead to an incomplete setup or errors during installation.
- gotcha There's a distinction between `python manage.py tailwind start` (for development with hot-reloading) and `python manage.py tailwind build` (for production static file generation). Using `start` in production will not generate optimized static assets.
Install
-
pip install django-tailwind
Imports
- django_tailwind
INSTALLED_APPS = ['django_tailwind', ...]
- TAILWIND_APP_NAME
TAILWIND_APP_NAME = 'theme'
- tailwind_tags
{% load tailwind_tags %} - tailwind_css
{% tailwind_css %}
Quickstart
# settings.py
INSTALLED_APPS = [
# ... your other apps
'django_tailwind',
'theme', # Your Tailwind app name
]
TAILWIND_APP_NAME = 'theme'
# urls.py (your project's main urls.py)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('django_tailwind.urls')),
# ... your other url patterns
]
# Your Tailwind app (e.g., 'theme/apps.py')
# Make sure to define your Tailwind app as a Django app
# For example:
# from django.apps import AppConfig
# class ThemeConfig(AppConfig):
# default_auto_field = 'django.db.models.BigAutoField'
# name = 'theme'
# In your base template (e.g., templates/base.html)
# Make sure to create a templates directory in your 'theme' app
# and an input.css in 'theme/static_src/input.css'
# {% load tailwind_tags %}
# <head>
# <title>My Django Tailwind App</title>
# {% tailwind_css %}
# </head>
# <body>
# <h1 class="text-3xl font-bold underline">Hello Tailwind!</h1>
# </body>
# Terminal commands
# python manage.py tailwind install
# python manage.py tailwind start
# (Open browser to your Django app, check console for Tailwind CSS build output)