-
Install Django with
pip install django
. -
Start a new Django project, with
django-admin startproject myproject
. -
Change directories into your project folder:
cd myproject
where the filemanage.py
is located. -
Double check that a Python interpreter has been selected in VS Code, with Ctrl + Shift + p.
-
Configure Django with Tailwind by following the instructions at Django-Tailwind.
-
Install the Django-Tailwind package with
python -m pip install django-tailwind
. -
Add
'tailwind'
toINSTALLED_APPS
insettings.py
:INSTALLED_APPS = [ # other Django apps 'tailwind', ]
-
Create a Tailwind CSS compatible Django app. Since this is for the benefit of Tailwind, i.e. css styling, it usually makes sense to call it
theme
. So, change directories into thenew_project
folder (wheremanage.py
is situated) and run the commandpython manage.py tailwind init
and at the prompt enter the nametheme
. Upon doing this you will see a new folder appear within your existing Django project. This folder will contain thetheme
Django app. -
Thus now we need to register this new
theme
Django app by adding it toINSTALLED_APPS
insettings.py
:INSTALLED_APPS = [ # other Django apps 'tailwind', 'theme' ]
-
Register the generated
theme
app by adding the following line tosettings.py
file (right at the bottom of thesettings.py
file):TAILWIND_APP_NAME = 'theme'
. -
Make sure that the
INTERNAL_IPS
list is present in thesettings.py
file and contains the127.0.0.1 ip
address. Again, place this at the bottom of thesettings.py
file:INTERNAL_IPS = [ "127.0.0.1", ]
-
-
Configure the path to the
npm
executable. OnWindows
use the command prompt and typewhere npm
and then copy the full path to thenpm.cmd
executable. Then, place that full path into thesettings.py
file, again right at the bottom:NPM_BIN_PATH = r"C:\Program Files\nodejs\npm.cmd"
. -
Add and configure
django_browser_reload
, which takes care of automatic page and css refreshes in the development mode. Add it toINSTALLED_APPS
insettings.py
:INSTALLED_APPS = [ # other Django apps 'tailwind', 'theme', 'django_browser_reload', ]
-
Add the middleware to the
MIDDLEWARE
section of thesettings.py
file: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
</body>
whenDEBUG
isTrue
. -
Include
django_browser_reload
URL in your rooturl.py
- also here, don't forget to import theinclude
function:from django.urls import include, path urlpatterns = [ ..., path("__reload__/", include("django_browser_reload.urls")), ]
-
Install Tailwind CSS dependencies, by running the following command:
python manage.py tailwind install
. Behind the scenes this will run thenpm
command. -
And at last you should be able to use Tailwind CSS classes in your Django HTML templates. Thus, start the development server by running the following command in your terminal:
python manage.py tailwind start
.
- There will be two servers that you need to run to make your life easier during further development of your Django project:
- The Tailwind server that will watch behind the scenes and update everytime changes are made to your CSS styling as you develop. This server is started in item 11 above:
python manage.py tailwind start
. - The Django development server:
python manage.py runserver
.
- The Tailwind server that will watch behind the scenes and update everytime changes are made to your CSS styling as you develop. This server is started in item 11 above:
-
We can now begin developing our Django project in exactly the same way as we would normally. Start by creating a new Django app with
python manage.py startapp myapp
. -
Add the name of your new app (e.g.
myapp
in this case) to yoursettings.py
file of the parent project folder:INSTALLED_APPS = [ ... 'tailwind', 'theme', 'django_browser_reload', 'myapp', ]
-
Also, add the routing to your root URLconf in
myproject/urls.py
:from django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), path("__reload__/", include("django_browser_reload.urls")), path('', include('myapp.urls')), ]
-
For templating, create a new folder called
templates
inside ofmyapp
folder. And then, inside oftemplates
create another new folder namedmyapp
. Finally create an empty file namedindex.html
insidetemplates/myapp
. -
Now, inside
myapp/views.py
we'll add the view for the aboveindex.html
file.def index(request): text = 'Hello World!' context = { 'context_text': text, } return render(request, 'myapp/index.html', context)
-
Back inside the
myapp
folder, create aurls.py
to store your URLconf. Inside there we define the following:from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
-
Finally we add some html to our
index.html
:{% load tailwind_tags %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Django Tailwind Example</title> {% tailwind_css %} </head> <body> <h1 class="text-2xl bg-slate-200 hover:bg-yellow-400">{{ context_text }}</h1> </body> </html>
-
Notice here we have added two new Tailwind specific tags to our html template, namely
{% load tailwind_tags %}
at the top of the template and{% tailwind_css %}
inside the<head>
tag. -
Now simply start the second server, i.e. the Django development server, with
python manage.py runserver
and visit the page.