ååã®ç¶ãã«ãªãã¾ã ããã£ã¨åé¿ãå°ãªãã£ãã®ã¯ã¿ã¤ãã«ãæªãã£ããã ãªï¼ã¨ãããã¨ã§ã¿ã¤ãã«å¤ãã¾ããã(ããããããããããã¨æãã¾ãããããããããããã§ããã¢ããã¼ã·ã§ã³ä¸ããã®ã§ã»ã»ã»)
ååã§ãç°å¢ãä½ããã¨ãã§ãã¾ãããä»åã¯ãã¼ã¸ãä½ã£ã¦ããã¾ãã
ååã¯ãåçµãããï¼ãã主ãªå 容ã§ããããä»åããã¯å¥½ããªãã®ãä½ã£ã¦ãããï¼ã主ç¼ã¨ããããã¨æãã¾ãã
ãã®åã«ã»ã»ã»Webãµã¼ãã¹ã£ã¦ã©ããã£ã¦åãã®ï¼
好ããªãã®ãä½ã£ã¦ãããï¼ã¨è¨ãã¾ããããã¾ãã¯Webãµã¼ãã¹ï¼ããã°ã©ã ï¼ã®æ¦å¿µçãªã¤ã¡ã¼ã¸ãæãã¦ããæ¹ãããã¨æãã¾ãã¦ã
åºæ¬çã«ãããã°ã©ã ã¯åç´åããã¨
å ¥åâè¨ç®/ä¿åâåºå
ã¨ããããã»ã¹ãçµããã¨ã«ãªãã¾ããããããWebã ã¨
ç»é¢ã§å ¥åãã¦âãµã¼ãã¼ã§è¨ç®/ä¿åãã¦âç»é¢ã«æç»ãã
ã¨ãã£ãå½¢ã«ãªãã¾ããç»é¢ãï¼ååºã¦ããã®ã§
- ç»é¢ãã©ãã¤ããã
- ãµã¼ãã¼ã§ã©ãè¨ç®/ä¿åããã
ããããã°ä½ãã¡ãããã¨ã«ãªãã¾ãã
å ¥åâè¨ç®/ä¿åâåºå
ãã®æµããã¤ã¡ã¼ã¸ãã¤ã¤ãä½ã£ã¦ããã¾ããã
ç»é¢ã®ä½æ
ååã¯ç»é¢ã½ãç»é¢ãããªãã£ãã§ãããä»åã¯ã¡ããã¨ç»é¢ãä½ã£ã¦ã¿ã¾ãã
ã©ãã«ä½ãå ¥åãã¦ãã©ãã«ä½ãåºåããã¤ã¡ã¼ã¸ãã¤ã¤ãHTMLãä½ã£ã¦ã¿ã¾ãã
HTMLãç½®ãå ´æã®ä½æ
ããã¸ã§ã¯ãã®ãã©ã«ãã®ä¸ãå³ã¯ãªãã¯ããNew Folderãé¸æãã¾ãã
ååããtemplatesãã¨ãã¦ããã©ã«ããä½æãã¦ãã ããã
次ã«ãä»ä½æãããã©ã«ããDjangoã«èªèããã¾ããsettings.pyãéããBASE_DIRã®ä¸ã«ä»¥ä¸ã®ä¸æã追å ãã¦ä¿åãã¾ãï¼templates以å¤ã®ååã§ãã©ã«ããä½æããå ´åã¯ãä¸ã®templatesã®é¨åãä½æãããã©ã«ãåã«å¤æ´ãã¦ãã ããï¼
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'), )
HTMLã®ä½æ
ç¶ãã¦ãå ã»ã©ä½ã£ãtemplatesãã©ã«ãã®ä¸ã«ãé©å½ãªååã§htmlãã¡ã¤ã«ãä½æãããã®ä¸ã«HTMLãæ¸ãã¾ããã©ãã«å ¥åããã¦ãã©ãã«åºåããããã¤ã¡ã¼ã¸ãã¤ã¤ãä¾ãã°ããããªæã
URLã¨å¦çãç´ã¥ãã¦ãHTMLãæå®ãã¦åºåãã
ç¶ãã¦ãä½æããHTMLãWebä¸ããè¦ããããã«ãã¾ãã
Webã¨ã¯ãè¨ãã¾ã§ããªãã§ããURLãéãã¦ã¢ã¯ã»ã¹ããã¾ãã
ä»ãDjangoä¸ã«ã¯HTMLãç½®ããã¦ãã¾ãããDjangoåã¯URLã«ã¢ã¯ã»ã¹ããã£ã¦ããã©ããªå¦çããã¦ã©ããªHTMLãè¿ãã°ããã®ãç¥ãã¾ããã
ãããæãã¦ããã¾ãã
ã¾ãã¯ãurls.pyãéãã¾ãã
ããã§ãååä½ã£ã
url(r'^top/$', 'testapp.views.main'),
ã®è¡ã«æ³¨ç®ãã¦æ¬²ãããã§ãããããã¯ä½ãæå³ãããã¨ããã¨ãæ£è¦è¡¨ç¾ã§ã^top/$ãã®URLã«ã¢ã¯ã»ã¹ããã£ãããtestappããã¸ã§ã¯ãã®views.pyã«ããmainé¢æ°ãå¼ã³åºããã¨ããæå³ã«ãªãã¾ãã
æ£è¦è¡¨ç¾ã¯ãã¾ã馴æã¿ããªãããããã¾ãããã^ã¯æååã®æåã表ãã$ã¯æååã®æå¾ã表ãã¦ã¾ããã¤ã¾ããtop/ã«ã¢ã¯ã»ã¹ãããã°mainé¢æ°ãå¼ã³åºãã¨ãªã£ã¦ãããã§ããã
ããã«ãæ°ãã«URLã追å ãã¾ããä¾ãã°ããããªæã
yakiniku/ã«ã¢ã¯ã»ã¹ãããã°ãharamié¢æ°ãå¼ã°ããããããªæ示ã«ãªãã¾ãã
å¦çãæ¸ã
haramié¢æ°ãä½ãã¾ããå®éã«è©¦ãå ´åã¯ããã£ã¨ã¾ã¨ããªååã«ãã¦ãã ãããã
views.pyãéããdef é¢æ°å (request): ã§é¢æ°ãå®ç¾©ãã¾ãã
次ã«ãå ã»ã©ä½ã£ãHTMLãæå®ãã¦ããã¾ãã
â ã¯ããã®ããã°ã©ã ã§RequestContextã¨ããé¨åã¨ãloaderã¨ããé¨åã使ãã¾ãããã¨ãã宣è¨ã§ãããã¾ããªãã ã¨æã£ã¦ãã ããã
â¡ã¯ãcontextã»ã»ã»ã§å§ã¾ãè¡ããç»é¢ã«è¿ãããå¤ã®æå®ãä»ã¯ç¹ã«å¤ãè¿ãããã§ããªãã®ã§ãã®ã¾ã¾åçµãã¦ãã ããã
templateã»ã»ã»ã§å§ã¾ãè¡ã¯ãå©ç¨ããhtmlã®æå®ã
returnã»ã»ã»ã§çµããè¡ã¯ãç»é¢æç»ãã¦ãã¨ããå½ä»¤ããã¡ãããã®ã¾ã¾åçµã§ã
ãªãããã®åç´¹ä»ããWebãµã¼ãã¹ã§ã¯ãviews.pyããããªæãã«ãªã£ã¦ãã¾ãã
è²ã ã¨ãã«ããã«ãæ¸ãã¦ãã¾ãããä¸ã®æ¹ã¯ä»åã¨ä¼¼ãæ§ãªæãã§æ¸ããã¦ããã®ãããããã¨æãã¾ãã
表示ãã¦ã¿ãã
ããã§ã¯å®éã«è¡¨ç¤ºãã¦ã¿ã¾ãããã
ãããä¸ã®ããã³ããã«(env)ãä»ãã¦ãªãç¶æ ã以ä¸ã®ç»åã®ãããªç¶æ ã ã£ããã»ã»ã»
ããä¸åº¦ã
source ~/workspace/venv/bin/activate
ã¨å ¥åãã¦ãããã³ããã®ç¶æ ãå¤æ´ãã¾ãã
ããã¯ä½ããã£ã¦ãããã¨ããã¨ãDjangoã¤ã³ã¹ãã¼ã«æã«è¨å®ããç°å¢ã®å¼ã³åºãããã¦ãã¾ãã
ä¾ãã°ããã°ã©ã ã«ãã£ã¦Djangoã®ãã¼ã¸ã§ã³ãå¤ãããæãªã©ãOSã«ã¤ã³ã¹ãã¼ã«ãã¦ãã¾ãã¨é½åãæªããªã£ã¦ãã¾ããããã§ãOSã«ç´æ¥ã¤ã³ã¹ãã¼ã«ããããªã³ãªããåãæ¿ãããããµã³ãããã¯ã¹ç°å¢çãªãã®ãä½æãã¦ã¤ã³ã¹ãã¼ã«ãã¦ããããã§ãã
ãã®ç°å¢ãå¼ã³åºãã¦ããã§ããã
次ã«ããµã¼ãã¼ãè½ã¡ã¦ãããããä¸åº¦èµ·åãã¦ããã¾ããmanage.pyãç½®ãã¦ããå ´æã«ç§»åãã
python manage.py runserver 0.0.0.0:8080
ãå®è¡ãã¦ããã¾ã
ããã§ãµã¼ãã¼ãèµ·åãã¾ããããå ã»ã©urls.pyã§æå®ããURLã«ã¢ã¯ã»ã¹ãã¦ã¿ãã¨ã»ã»ã»
ã¡ããã¨HTMLã表示ããã¦ãã¾ãï¼
Â
ã»ã»ã»ã¨ä»åã¯ããã¾ã§ã«ãã¦ã次åã¯ããã«å¦çãå ãã¦ããããã¨æãã¾ãã
Â
Â
<追è¨>
æçµç« æ¸ãã¾ããï¼