Djangoãã¬ã¼ã ã¯ã¼ã¯ã«ã¯ãã³ãã¬ã¼ãã¨ã³ã¸ã³ãå¥ã®ãã®ã«åãæ¿ããæ©è½ãããã¾ãã Django Cottonã¨ãããã³ãã¬ã¼ãã¨ã³ã¸ã³ã試ãã¦ã¿ãã®ã§ãã¡ã¢ãæ®ãã¦ããã¾ãã django-cotton.com GitHub - wrabit/django-cotton: Enabling Modern UI Composition in Django
Django Cottonã¯ãã³ã³ãã¼ãã³ã(é¨å)ã¨ãªããã³ãã¬ã¼ããä½ã£ã¦çµã¿åããã¦ãããã³ã³ãã¼ãã³ãæåã®ãã³ãã¬ã¼ãè¨è¨ãå®ç¾ãããã³ãã¬ã¼ãã¨ã³ã¸ã³ã§ãã
ããã¥ã¡ã³ãã®èª¬æã«ã¯ã TailwindCSS (ã³ã³ãã¼ãã³ãæåã®CSSãã¬ã¼ã ã¯ã¼ã¯)ã¨ãããã¦ä½¿ãããããã¨ãæ¸ããã¦ãã¾ãã
ããã¥ã¡ã³ãã® Quickstart ãåèã«ãªãã¾ãã
以ä¸ã django-admin startproject myproject
ã§ä½æããDjangoããã¸ã§ã¯ãã«ã»ããã¢ãããã¦è©¦ãã¦ããã¾ãã
ã»ããã¢ãã
PyPIä¸ã®ããã±ã¼ã¸å㯠django-cotton
ã§ããpipã§ã¤ã³ã¹ãã¼ã«ã§ãã¾ãã
pip install django-cotton
ããã©ã«ãã®è¨å®ã§ä½¿ãã ããªãã settings.INSTALLED_APPS
㸠django_cotton
ã追å ãã¾ãã
myproject/settings.py:
INSTALLED_APPS = [ # ... 'django_cotton', ]
â»ããã©ã«ãè¨å®ã®å ´åãdjango_cottonã®AppConfigããã¼ããããéã«ãDjangoã®settingsã®ãã³ãã¬ã¼ãè¨å®ãå¤æ´ããå¦çãå®è¡ããã¾ãããã³ãã¬ã¼ãè¨å®ãèªåã§å¤æ´ãã¦ããå ´åã«ã¯ãä¸å ·åãåºã¦ããªããããã§ãã¯ãã¦ãããã»ããããããã§ãã
åè: django_cotton/apps.py
ãããããã©ã«ãè¨å®ä»¥å¤ã使ãããå ´åã¯ã djanog_cotton.apps.SimpleAppConfig
ã®ã»ãã INSTALLED_APPS
ã«è¨å®ã§ãã¾ãã
ã試ãç¨ã«Djangoã¢ããªãæºåãã
myappã¨ããååã®Djangoã¢ããªã±ã¼ã·ã§ã³ãä½æãã¾ãã
python manage.py startapp myapp
INSTALLED_APPSã«ã追å ãã¦ããã¾ãã
myproject/settings.py:
INSTALLED_APPS = [ # ... 'django_cotton', 'myapp', # 追å ]
TailwindCSSã®CDNãçµã¿è¾¼ã
TailwindCSSã§å®ç¾©ããã¦ããCSSã¯ã©ã¹ã使ç¨ããã®ã§ãCDNãããã¼ãããããã«ãã³ãã¬ã¼ããä½ã£ã¦ããã¾ãã
Try Tailwind CSS using the Play CDN - Tailwind CSS
â»æ¬çªç°å¢ã§ä½¿ãå ´åã¯Tailwindã¯ãã«ããã¦ä½¿ã£ãã»ããããã§ãã
myapp/templates/index.html:
<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://cdn.tailwindcss.com"></script> </head> <body> <div class="p-2"> <h1 class="text-3xl font-bold underline"> Django Cotton! </h1> </div> </body> </html>
Viewã¯index.htmlã使ãã ãã®ã·ã³ãã«ãªè¨è¿°
myapp/views.py:
from django.shortcuts import render def index(request): return render(request, 'index.html')
ããã¸ã§ã¯ãã®urls.pyã§indexãæå¹ã«ãã¾ãã
myproject/urls.py:
from django.urls import path from myapp.views import index urlpatterns = [ path('', index), ]
runserverãèµ·åãã¦ç¢ºèªãã¦ããã¾ãã
TailwindCSSã®CSSãé©ç¨ããã¦ãããã¨ã確èªã§ãã¾ããã
ã³ã³ãã¼ãã³ãã¨ãªããã³ãã¬ã¼ããä½ã
ã«ã¼ã表示ã®ã³ã³ãã¼ãã³ããä½ã£ã¦ã¿ã¾ãã
Django Cottonã®ããã©ã«ãè¨å®ã®å ´åãã³ã³ãã¼ãã³ãã¯templatesãã£ã¬ã¯ããªå ã«cottonã¨ãããã£ã¬ã¯ããªãä½ã£ã¦æ ¼ç´ãã¾ãã
myapp/templates/cotton/card.html:
<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">{{ title }}</div> <p class="text-gray-700 text-base">{{ slot }}</p> </div> <div class="px-6 pt-2 pb-2"> <a href="{{ url }}" 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>
{{ title }}
ã {{ url }}
ã®ãã¬ã¼ã¹ãã«ãã¼ã¯ãã³ã³ãã¼ãã³ãã®attribute(å±æ§)ã§æå®ã§ããããã«ãã¦ãã¾ãã
{{ slot }}
ã®é¨åã¯Cottonã§äºç´ãããå称ã§ãã³ã³ãã¼ãã³ãå¼ã³åºãå´ã®ã¿ã°ã®å
å´ã«è¨è¼ããå
容ãå·®ãè¾¼ã¾ãã¾ãã
ã³ã³ãã¼ãã³ãã使ç¨ãã
ä½æãã¦ããã index.html ãæ¸ãæãã¾ãã
templates/cotton/card.html
ã«ã³ã³ãã¼ãã³ããä½æããã®ã§ã <c-card></c-card>
ã¨ããã¿ã°ã§å¼ã³åºãã¾ãã
myapp/templates/index.html:
<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://cdn.tailwindcss.com"></script> </head> <body> <div class="p-2"> <h1 class="text-3xl font-bold underline"> Django Cotton! </h1> <c-card title="ã«ã¼ãã¿ã¤ãã«" url="http://example.com/"> ã«ã¼ãæ¬æ </c-card> <c-card title="ã«ã¼ã2ã¿ã¤ãã«" url="http://example.com/"> ã«ã¼ã2æ¬æ </c-card> </div> </body> </html>
ç»é¢ã§ç¢ºèªããã¨ãã³ã³ãã¼ãã³ãã使ç¨ããã¦ãã«ã¼ã表示ãã§ãã¦ãã¾ãã
ããã§ãDjango Cottonãçµã¿è¾¼ãã å®è£ ãã§ãã¾ããã
ããã³ãã¨ã³ãã®ã³ã¼ãã£ã³ã°ã§é¦´æã¿ã®ãããããªãã³ãã¬ã¼ãã®æ¸ãæ¹ã«ã§ããã®ã§é¢ç½ãã§ããã