{"id":682,"date":"2019-01-20T18:06:02","date_gmt":"2019-01-20T09:06:02","guid":{"rendered":"https:\/\/www.nblog09.com\/w\/?p=682"},"modified":"2021-11-11T17:31:51","modified_gmt":"2021-11-11T08:31:51","slug":"djngo-template","status":"publish","type":"post","link":"https:\/\/www.nblog09.com\/w\/2019\/01\/20\/djngo-template\/","title":{"rendered":"Django\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d5\u30a1\u30a4\u30eb\u914d\u7f6e\u5148\u30c7\u30a3\u30ec\u30af\u30c8\u306e\u8a2d\u5b9a(TEMPLATES)"},"content":{"rendered":"<h2 class=\"title-border\">Django\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d5\u30a1\u30a4\u30eb\u914d\u7f6e\u5148\u30c7\u30a3\u30ec\u30af\u30c8\u306e\u8a2d\u5b9a<\/h2>\n<p>Django\u3067\u3001View\u3067\u51e6\u7406\u3057\u305f\u5185\u5bb9\u3092\u8868\u793a\u3059\u308b\u753b\u9762\u3092\u4f5c\u6210\u3059\u308b\u306e\u304c\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3002<\/p>\n<p>\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u30d5\u30a1\u30a4\u30eb\u306e\u914d\u7f6e\u5148\u306f\u3001<a href=\"https:\/\/www.nblog09.com\/w\/2019\/01\/15\/django-setting-file\/\">\u4ee5\u524d\u306e\u8a18\u4e8b<\/a>\u3067\u3082\u8a18\u8f09\u3057\u305fsettings.py\u306e\u3001<span class=\"pink_span\">TEMPLATES\u5909\u6570\u306b\u8a2d\u5b9a\u3059\u308b<\/span>\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306bsettings.py\u306eBASE_DIR\u306b\u5408\u308f\u305b\u3066\u8a18\u8f09\u3059\u308b\u3053\u3068\u3067\u3001<span class=\"pink_span\">\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30d5\u30a9\u30eb\u30c0\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d5\u30a9\u30eb\u30c0\u3068\u540c\u3058\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u4e0a\u306b\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b<\/span>\u3002<\/p>\n<div class=\"programming-gray\">\n<h4>Example<\/h4>\n<ul>\n<li>settings.py\u3067\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d5\u30a9\u30eb\u30c0\u306e\u914d\u7f6e\u5148\u3092\u8a2d\u5b9a\u3059\u308b<\/li>\n<\/ul>\n<div class=\"programming-inside\">\n<strong># settings.py<\/strong><\/p>\n<p>import os <strong># os\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u30a4\u30f3\u30dd\u30fc\u30c8<\/strong><\/p>\n<p>BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) <strong># BASE_DIR\u306fproject\u3092\u4f5c\u6210\u3057\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea(manage.py\u306e\u3042\u308b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea)<\/strong><\/p>\n<p><span class=\"codehighlight\">TEMPLATE_DIR<\/span> = os.path.join(BASE_DIR,'templates') <strong># TEMPLATE_DIR\u306fBASE_DIR\/templates\u30d5\u30a9\u30eb\u30c0\u5185\u3092\u8868\u793a\u3059\u308b<\/strong>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u6b21\u306b\u3001TEMPLATE_DIR\u3092\u8a2d\u5b9a\u3057\u305f\u3089\u3001TEMPLATES\u5909\u6570\u306b\u8a18\u5165\u3059\u308b<\/p>\n<ul>\n<li>TEMPLATES\u5909\u6570\u306b\u6307\u5b9a\u3059\u308b<\/li>\n<\/ul>\n<div class=\"programming-inside\">\n<strong># settings.py<\/strong><\/p>\n<p><span class=\"codehighlight\">TEMPLATES<\/span> = [<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'BACKEND': 'django.template.backends.django.DjangoTemplates',<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'DIRS': [TEMPLATE_DIR,], <strong># TEMPLATE_DIR\u8a2d\u5b9a<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'APP_DIRS': True,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'OPTIONS': {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'context_processors': [<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'django.template.context_processors.debug',<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'django.template.context_processors.request',<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'django.contrib.auth.context_processors.auth',<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'django.contrib.messages.context_processors.messages',<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;},<br \/>\n]<\/div>\n<\/div>\n<p>\u5f8c\u306f\u3001\u5b9f\u969b\u306bBASE_DIR\uff08\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30d9\u30fc\u30b9\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\uff09\u76f4\u4e0b\u306btemplates\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4f5c\u6210\u3057\u3066\u3001\u4e2d\u306bhtml\u30d5\u30a1\u30a4\u30eb\u306a\u3069\u3092\u5165\u308c\u3066\u3044\u304f\u3068\u3001\u30d3\u30e5\u30fc\u306erender('\u3007\u3007.html')\u3067<span class=\"pink_span\">\u5404templates\u306b\u914d\u7f6e\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b<\/span><\/p>\n<div class=\"programming-gray\">\n<h4>Example<\/h4>\n<ul>\n<li>\u4e0a\u8a18\u8a2d\u5b9a\u3092\u3057\u305f\u5834\u5408\u306e\u30d5\u30a9\u30eb\u30c0\u69cb\u6210\uff08\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3068\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u540c\u3058\u968e\u5c64\u306btemplates\u30d5\u30a9\u30eb\u30c0\u3092\u7f6e\u304f\uff09<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/1-3.png\" alt=\"\" width=\"300\" height=\"339\" class=\"alignnone size-full wp-image-2379\" \/><br \/>\n&nbsp;<\/p>\n<ul>\n<li>views.py\u306btemplates\u3092\u8aad\u307f\u8fbc\u3080html\u3092\u6307\u5b9a\u3059\u308b<\/li>\n<\/ul>\n<div class=\"programming-inside\">\ndef index(request):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return <span class=\"codehighlight\">render<\/span>(request, 'index.html') <strong># templates\u30d5\u30a9\u30eb\u30c0\u5185\u306eindex.html\u3092\u8aad\u307f\u8fbc\u3080<\/strong><\/p>\n<p>def index2(request):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return <span class=\"codehighlight\">render<\/span>(request, 'app\/index.html') <strong># templates\u30d5\u30a9\u30eb\u30c0\u5185\u306eapp\u30d5\u30a9\u30eb\u30c0\u5185\u306eindex.html\u3092\u8aad\u307f\u8fbc\u3080<\/strong>\n<\/div>\n<\/div>\n<div class=\"django-adsense\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Django\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d5\u30a1\u30a4\u30eb\u914d\u7f6e\u5148\u30c7\u30a3\u30ec\u30af\u30c8\u306e\u8a2d\u5b9a Django\u3067\u3001View\u3067\u51e6\u7406\u3057\u305f\u5185\u5bb9\u3092\u8868\u793a\u3059\u308b\u753b\u9762\u3092\u4f5c\u6210\u3059\u308b\u306e\u304c\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3002 \u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u30d5\u30a1\u30a4\u30eb\u306e\u914d\u7f6e\u5148\u306f\u3001\u4ee5\u524d\u306e\u8a18\u4e8b\u3067\u3082\u8a18\u8f09\u3057\u305fsettings.py\u306e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":257,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[121,219],"tags":[122,14,125],"class_list":{"0":"post-682","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-django","8":"category-it-programming","9":"tag-django","10":"tag-python","11":"tag-template","12":"entry"},"uagb_featured_image_src":{"full":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/desk-2158142__480.jpg",720,480,false],"thumbnail":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/desk-2158142__480-150x150.jpg",150,150,true],"medium":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/desk-2158142__480-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/desk-2158142__480.jpg",720,480,false],"large":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/desk-2158142__480.jpg",720,480,false],"1536x1536":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/desk-2158142__480.jpg",720,480,false],"2048x2048":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/desk-2158142__480.jpg",720,480,false],"home-middle":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/desk-2158142__480.jpg",525,350,false],"home-top":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/desk-2158142__480.jpg",630,420,false],"sidebar-thumbnail":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/desk-2158142__480.jpg",100,67,false]},"uagb_author_info":{"display_name":"9nmatsumoto","author_link":"https:\/\/www.nblog09.com\/w\/author\/9nmatsumoto\/"},"uagb_comment_info":4,"uagb_excerpt":"Django\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d5\u30a1\u30a4\u30eb\u914d\u7f6e\u5148\u30c7\u30a3\u30ec\u30af\u30c8\u306e\u8a2d\u5b9a Django\u3067\u3001View\u3067\u51e6\u7406\u3057\u305f\u5185\u5bb9\u3092\u8868\u793a\u3059\u308b\u753b\u9762&hellip;","_links":{"self":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/682","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/comments?post=682"}],"version-history":[{"count":13,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/682\/revisions"}],"predecessor-version":[{"id":3780,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/682\/revisions\/3780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media\/257"}],"wp:attachment":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media?parent=682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/categories?post=682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/tags?post=682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}