CSSãã¬ã¼ã ã¯ã¼ã¯ã§ãã TailwindCSS ã¯ãé常NodeJSã使ã£ã¦CSSããã«ããã¾ãã
Djangoããå©ç¨ããå ´åãTailwindCSSã®ãã«ãç°å¢ãæ´åããä¸ã§ããã«ããããCSSãDjangoã§èªã¿è¾¼ãããã«å®è£ ãå¿ è¦ã«ãªãã¾ãã
django-tailwindã使ãã¨ããã®ã»ããã¢ããã¨Djangoã¸ã®çµã¿è¾¼ã¿ãæ軽ã«å®æ½ã§ãã¾ãã
django-tailwind.readthedocs.io
以ä¸ã django-admin startproject myproject
ã§ä½æããmyprojectã§django-tailwindã試ãã¾ãã
ã¤ã³ã¹ãã¼ã«ã¨è¨å®
django-tailwindã¯å
é¨ã§ npm
ã³ãã³ããå®è¡ãããããããããã NodeJS ãã¤ã³ã¹ãã¼ã«ãã¦ããå¿
è¦ãããã¾ãã
pipã§django-tailwindãã¤ã³ã¹ãã¼ã«ãã¾ãã
pip install django-tailwind
tailwind
ã¢ããªãsettings.pyã® INSTALLED_APPS
ã«è¿½å ãã¾ãã
INSTALLED_APPS = [ # ... 'tailwind', ]
ãã㧠manage.py tailwind
ã³ãã³ãããdjango-tailwindãæä¾ãããã³ãã¬ã¼ãã¿ã°ãæå¹ã«ãªãã¾ããã
次ã«TailwindCSSã®ãã¡ã¤ã«ç¾¤ãæ ¼ç´ãããã¼ãã¢ããªãä½æãã¾ãã
manage.py tailwind init
ãå®è¡ããã¨ãcookiecutterãã¤ã³ã¹ãã¼ã«ããã¦ãcookiecutterã§ãã¼ãã¢ããªãä½ããã¾ãã
python manage.py tailwind init
ã¢ããªåã¯ããã©ã«ãã®ã¾ã¾ã 㨠theme
ã¨ããååã«ãªãã¾ãã
ä½æãã theme
ã¢ããªã INSTALLED_APPS
ã«è¿½å ãã¾ãã
INSTALLED_APPS = [ # ... 'tailwind', 'theme', # 追å ]
settings.pyã« TAILWIND_APP_NAME
ã®è¨å®ã追è¨ãã¾ããå¤ã¯ä½æãããã¼ãã®ã¢ããªåã§ãã
TAILWIND_APP_NAME = 'theme'
settings.pyã« INTERNAL_IPS
ã®è¨å®ã追å ãã¾ããä»åã¯éçºç¨ãªã®ã§ãlocalhostã®ã¢ãã¬ã¹ã ããè¨å®ãã¾ãã
INTERNAL_IPS = [
'127.0.0.1',
]
TailwindCSSã®ä¾åé¢ä¿ãã¤ã³ã¹ãã¼ã«ãã¾ãã
python manage.py tailwind install
ä¸è¨ã®ã³ãã³ãã¯ãå
é¨ã§ npm install
ãå®è¡ãã¾ãã
ããã§ã¤ã³ã¹ãã¼ã«ã¨å©ç¨è¨å®ã¯å®äºã§ãã
ãã³ãã¬ã¼ããä½æãã¦å©ç¨ãã
ããã©ã«ãã§ã¯ theme/templates/base.html
ãä½æããã¦ãã¾ãããããã¯ç·¨éå¯è½ã§ããã¾ããã¹ãtemplatesç´ä¸ã§ãããã¨ã«æ°ãã¤ãã¦ãããã»ããããããã§ããä»ã®ã¢ããªã®ããã« base.html
ãç¨æãã¦ããå ´åãè¡çªããå¯è½æ§ãããã¾ãã
theme/templates/base.html
:
{% load static tailwind_tags %} <!DOCTYPE html> <html lang="ja"> <head> <title>{% block title %}{% endblock %}</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> {% tailwind_css %} </head> <body class="bg-gray-50 font-serif leading-normal tracking-normal"> <div class="container mx-auto"> <section class="p-4"> <h1 class="text-5xl">{% block h1 %}{% endblock %}</h1> </section> {% block main %}{% endblock %} </div> </body> </html>
ãã® base.html
ãå©ç¨ãããã¼ã¸ãä½ã£ã¦ã¿ã¾ãã
myappã¨ããã¢ããªãä½æãã¾ãã
python manage.py startapp myapp
INSTALLED_APPS
ã«ã追å ãã¦ããã¾ãã
INSTALLED_APPS = [ # ... 'tailwind', 'theme', 'myapp', # 追å ]
templatesãã£ã¬ã¯ããªãä½æãindex.htmlãã¡ã¤ã«ãä½æãã¾ãã
myapp/templates/index.html:
{% extends "base.html" %} {% block title %}Django Tailwind{% endblock %} {% block h1 %}Django + Tailwind{% endblock %} {% block main %} <div class="max-w-sm rounded overflow-hidden shadow-lg border mb-2"> <div class="px-6 py-4"> <div class="font-bold text-xl mb-2">ã«ã¼ãã¿ã¤ãã«</div> <p class="text-gray-700 text-base">ã«ã¼ãæ¬æ</p> </div> <div class="px-6 pt-2 pb-2"> <a href="http://example.com/" class="bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-blue-700 mr-2 mb-2">Read more</a> </div> </div> {% endblock %}
index.html
ãå©ç¨ãããã¥ã¼ãç¨æãã¾ãã
myapp/views.py:
from django.shortcuts import render def index(request): return render(request, 'index.html')
ãã¥ã¼ãããã¸ã§ã¯ãã®urls.pyã«ç»é²ãã¾ãã
myproject/urls.py:
from django.urls import path from myapp.views import index urlpatterns = [ path('', index), ]
ããã¾ã§ã§ãããåä½ç¢ºèªããã¾ãã
tailwindã®éçºç¨ã®ãã«ãã¯ã manage.py tailwind start
ãå®è¡ããã¨ãdjango-tailwindãnpmã³ãã³ããå®è¡ãã¦ããã¾ãã
python manage.py tailwind start
tailwindã®ãã«ããèµ·åããç¶æ ã§ãDjangoã®runserverãèµ·åãããã©ã¦ã¶ã§ç¢ºèªãã¦ã¿ã¾ãã
python manager.py runserver
TailwindCSSãé©ç¨ããããã¼ã¸ã表示ã§ãã¾ããã
django-tailwindã使ãã¨ãTailwindCSSãDjangoã«æ°è»½ã«çµã¿è¾¼ããã®ãããããã§ããã