{"id":686,"date":"2019-01-20T18:30:51","date_gmt":"2019-01-20T09:30:51","guid":{"rendered":"https:\/\/www.nblog09.com\/w\/?p=686"},"modified":"2021-11-11T17:32:11","modified_gmt":"2021-11-11T08:32:11","slug":"django-view-template","status":"publish","type":"post","link":"https:\/\/www.nblog09.com\/w\/2019\/01\/20\/django-view-template\/","title":{"rendered":"Django\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3067\u30d3\u30e5\u30fc\u304b\u3089\u6e21\u3055\u308c\u305f\u5909\u6570\u3092\u8868\u793a\u3059\u308b\u65b9\u6cd5\u8a73\u7d30\uff5e\u30ea\u30b9\u30c8\u3001\u8f9e\u66f8\u306a\u3069\uff5e"},"content":{"rendered":"<div style=\"background: #ffffff; padding: 15px; border: 2px solid #99cc00; border-radius: 10px; word-break: break-all;\">\n<p>&nbsp;<\/p>\n<ul>\n<li>\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3067\u30d3\u30e5\u30fc\u304b\u3089\u6e21\u3055\u308c\u305f\u5909\u6570\u3092\u8868\u793a\u3059\u308b<\/li>\n<li>\u30ea\u30b9\u30c8\u578b\u306e\u5909\u6570\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u6307\u5b9a\u3057\u3066\u4e00\u90e8\u3092\u8868\u793a<\/li>\n<li>\u30ea\u30b9\u30c8\u578b\u306e\u5909\u6570\u3092\u30eb\u30fc\u30d7\u3055\u305b\u308b<\/li>\n<li>\u8f9e\u66f8\u578b\u306e\u5909\u6570\u306e\u30ad\u30fc\u3092\u6307\u5b9a\u3057\u3066\u8868\u793a<\/li>\n<li>\u8f9e\u66f8\u578b\u306e\u5909\u6570\u3092items\u30e1\u30bd\u30c3\u30c9\u3067\u30eb\u30fc\u30d7\u3055\u305b\u30ad\u30fc\u3068\u5024\u3092\u53d6\u308a\u51fa\u3059<\/li>\n<\/ul>\n<p>\u306a\u3069\u3001Django\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u3001\u8a73\u7d30\u306b\u89e3\u8aac\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<\/div>\n<h2 class=\"title-border\"><strong>Django\u306e\u30d3\u30e5\u30fc\u304b\u3089\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u5024\u3092\u6e21\u3059\u65b9\u6cd5<\/strong><\/h2>\n<p>\u4ee5\u524d\u306e\u8a18\u4e8b\u3067<a href=\"https:\/\/www.nblog09.com\/w\/2019\/01\/20\/djngo-template\/\">\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d5\u30a1\u30a4\u30eb\u306e\u914d\u7f6e\u5148\u30c7\u30a3\u30ec\u30af\u30c8\u30ea<\/a>\u3068<a href=\"https:\/\/www.nblog09.com\/w\/2019\/01\/20\/django-mapping\/\">URL\u30de\u30c3\u30d4\u30f3\u30b0<\/a>\u306b\u3064\u3044\u3066\u8a18\u8f09\u3057\u305f\u304c\u3001\u4eca\u56de\u306f<span class=\"pink_span\">\u30d3\u30e5\u30fc\u3067\u4f5c\u6210\u3057\u305f\u5909\u6570\u3092\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d5\u30a1\u30a4\u30eb\u306b\u6e21\u3057\u3066\u8868\u793a\u3059\u308b\u65b9\u6cd5<\/span>\u3092\u8aac\u660e\u3059\u308b\u3002<\/p>\n<p>\u30d3\u30e5\u30fc\u306b\u6587\u5b57\u5217\u3001\u6570\u5024\u3092\u6e21\u3059\u5834\u5408\u306b\u306frender\u3067<span class=\"pink_span\">context=<\/span>\u3068\u3057\u3066\u8a18\u8f09\u3059\u308b\u3002<\/p>\n<p>(TEMPLATE_DIR\u306e\u8a2d\u5b9a\u306f<a href=\"https:\/\/www.nblog09.com\/w\/2019\/01\/20\/djngo-template\/\">\u3053\u3061\u3089<\/a>)<\/p>\n<h3 class=\"under-blue\"><strong>\uff11: \u6587\u5b57\u5217\u3001\u6570\u5024\u578b\u3092\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u6e21\u3057\u3066\u8868\u793a<\/strong><\/h3>\n<div class=\"programming-gray\">\n<ul>\n<li>views.py\u306b\u8a18\u8ff0\u3059\u308b\u5185\u5bb9<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django.shortcuts import render<\/p>\n<p>def index(request):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;name = 'Taro'<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;age = 20<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return <span class=\"codehighlight\">render<\/span>(request, 'firstapp\/index.html', <span class=\"codehighlight\">context<\/span>={<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'<span class=\"codehighlight\">var_name<\/span>': name, <strong># \u5909\u6570name(Taro)\u306e\u5024\u3092var_name\u3068\u3044\u3046\u540d\u524d\u3067\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u9001\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'<span class=\"codehighlight\">var_age<\/span>': age,<strong># \u5909\u6570age(20)\u306e\u5024\u3092var_name\u3068\u3044\u3046\u540d\u524d\u3067\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u9001\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<strong> #TEMPLATE_DIR\/firstapp\/index.html\u3092\u8868\u793a\u3059\u308b\u3002context\u306b\u8a2d\u5b9a\u3057\u305f\u5909\u6570\u3092\u5024\u3092template\u306b\u6e21\u3059\u3002<\/strong>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>TEMPLATE_DIR\/firstapp\/index.html\u3067views.py\u304b\u3089\u6e21\u3055\u308c\u305f\u5024\u3092\u8868\u793a\u3059\u308b\u3002<\/li>\n<\/ul>\n<div class=\"programming-inside\">\n&lt;!DOCTYPE html&gt;<br \/>\n&lt;html&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;head&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta charset=\"utf-8\"&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;First App&lt;\/title&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/head&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: {{ <span class=\"codehighlight\">var_name<\/span> }}, age: {{ <span class=\"codehighlight\">var_age<\/span> }}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>django\u3092\u8d77\u52d5\u3057\u3066\u3001url\u3067\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8868\u793a\u3055\u308c\u308b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/1-4.png\" alt=\"\" width=\"600\" height=\"359\" class=\"alignnone size-full wp-image-2382\" \/>\n<\/div>\n<h3 class=\"under-blue\"><strong>\uff12: \u30ea\u30b9\u30c8\u578b\u3092\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u6e21\u3057\u3066\u8868\u793a<\/strong><\/h3>\n<p>\u30ea\u30b9\u30c8\u578b\u3067\u3082\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u5024\u3092\u6e21\u3057\u3066\u8868\u793a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>views.py\u306b\u8a18\u8ff0\u3059\u308b\u5185\u5bb9<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django.shortcuts import render<\/p>\n<p>def index(request):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;fruits = ['apple', 'lemon', 'grape']<strong># \u30ea\u30b9\u30c8\u3067fruits\u5909\u6570\u3092\u4f5c\u6210<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return <span class=\"codehighlight\">render<\/span>(request, 'index.html', <span class=\"codehighlight\">context<\/span>={<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'<span class=\"codehighlight\">fruits<\/span>': fruits, <strong># \u30ea\u30b9\u30c8\u578b\u306efruits\u3092\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u9001\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;})<\/p>\n<\/div>\n<p>&nbsp;<br \/>\n\u6b21\u306b\u3001\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8868\u793a\u3059\u308b\u3002<\/p>\n<ul>\n<li>index.html\u306b\u8a18\u8ff0\u3059\u308b\u5185\u5bb9<\/li>\n<\/ul>\n<div class=\"programming-inside\">\n{{ <span class=\"codehighlight\">fruits<\/span> }}&lt;<strong>!-- \u30ea\u30b9\u30c8\u306e\u5185\u5bb9['apple', 'lemon', 'grape'] \u304c\u8868\u793a --&gt;<\/strong><br \/>\n{{ <span class=\"codehighlight\">fruits.1<\/span> }} &lt;<strong>!-- index\u304c1\u306e\u5024(lemon)\u304c\u8868\u793a\u3055\u308c\u308b--&gt;<\/strong><br \/>\n{% <span class=\"codehighlight\">for fruit in fruits<\/span> %} &lt;<strong>!-- \u30ea\u30b9\u30c8\u3092\u30eb\u30fc\u30d7\u3055\u305b\u308b --&gt;<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;{{ fruit }}&lt;\/p&gt; &lt;<strong>!-- \u30ea\u30b9\u30c8\u306e\u4e2d\u8eab\u30921\u3064\u305a\u3064\u8868\u793a\u3059\u308b --&gt;<\/strong><br \/>\n{% endfor %}\n<\/div>\n<\/div>\n<h3 class=\"under-blue\"><strong>\uff13: \u8f9e\u66f8\u578b\u3092\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u6e21\u3057\u3066\u8868\u793a<\/strong><\/h3>\n<p>\u8f9e\u66f8\u578b\u3067\u3082\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u5024\u3092\u6e21\u3057\u3066\u8868\u793a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br \/>\n<span class=\"pink_span\">.\u30ad\u30fc\u540d\u3068\u3057\u3066\u3001\u8f9e\u66f8\u578b\u306e\u5404\u5024\u306b\u30ad\u30fc\u3092\u6307\u5b9a\u3057\u3066\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b<\/span>\u3002<br \/>\n\u307e\u305f\u3001<span class=\"pink_span\">\u30eb\u30fc\u30d7\u6587\u3068items\u30e1\u30bd\u30c3\u30c9\u3092\u7528\u3044\u308b\u3053\u3068\u3067\u3001\u30ad\u30fc\u3068\u30d0\u30ea\u30e5\u30fc\u3092\u305d\u308c\u305e\u308c\u53d6\u308a\u51fa\u3057\u3066\u8868\u793a\u3059\u308b\u3053\u3068<\/span>\u3082\u3067\u304d\u308b\u3002<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>views.py\u306b\u8a18\u8ff0\u3059\u308b\u5185\u5bb9<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django.shortcuts import render<\/p>\n<p>def index(request):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;user = { <strong># \u8f9e\u66f8\u578b\u3067\u5909\u6570\u3092\u5b9a\u7fa9\u3059\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'name': 'Taro',<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'age': 20,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return render(request, 'index.html', context={<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'<span class=\"codehighlight\">user<\/span>': user, <strong># \u8f9e\u66f8\u578b\u306e\u5909\u6570\u3092\u6e21\u3059<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;})<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>index.html\u306b\u8a18\u8ff0\u3059\u308b\u5185\u5bb9<\/li>\n<\/ul>\n<div class=\"programming-inside\">\n{{ <span class=\"codehighlight\">user.name<\/span> }}<strong>&lt;!-- user\u306ename\u306b\u30a2\u30af\u30bb\u30b9(Taro\u3068\u8868\u793a) --&gt;<\/strong><br \/>\n{{ <span class=\"codehighlight\">user.age<\/span> }}<strong>&lt;!-- user\u306eage\u306b\u30a2\u30af\u30bb\u30b9(20\u3068\u8868\u793a) --&gt;<\/strong><\/p>\n<p>{% <span class=\"codehighlight\">for key, value in user.items<\/span> %} <strong>&lt;!-- user\u3092\u30eb\u30fc\u30d7\u3057\u3066\u3001\u30ad\u30fc\u3068\u30d0\u30ea\u30e5\u30fc\u3092key, value\u306b\u5165\u308c\u308b --&gt;<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;{{key}} - {{value}}&lt;\/p&gt;<strong>&lt;!-- name - Taro, age - 20\u3092\u8868\u793a --&gt;<\/strong><br \/>\n{% endfor %}<\/p>\n<p>{% if 'name' in user %}<strong>&lt;!-- \u30ad\u30fcname\u304cuser\u306b\u5b58\u5728\u3059\u308b\u304b\u3069\u3046\u304b --&gt;<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;name\u306fuser\u306b\u5b58\u5728\u3057\u307e\u3059&lt;\/p&gt;<br \/>\n{% endif %}\n<\/div>\n<\/div>\n<div class=\"django-adsense\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3067\u30d3\u30e5\u30fc\u304b\u3089\u6e21\u3055\u308c\u305f\u5909\u6570\u3092\u8868\u793a\u3059\u308b \u30ea\u30b9\u30c8\u578b\u306e\u5909\u6570\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u6307\u5b9a\u3057\u3066\u4e00\u90e8\u3092\u8868\u793a \u30ea\u30b9\u30c8\u578b\u306e\u5909\u6570\u3092\u30eb\u30fc\u30d7\u3055\u305b\u308b \u8f9e\u66f8\u578b\u306e\u5909\u6570\u306e\u30ad\u30fc\u3092\u6307\u5b9a\u3057\u3066\u8868\u793a \u8f9e\u66f8\u578b\u306e\u5909\u6570\u3092items\u30e1\u30bd\u30c3\u30c9\u3067\u30eb\u30fc\u30d7\u3055\u305b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":354,"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":[126,14,125,127],"class_list":{"0":"post-686","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-django","8":"category-it-programming","9":"tag-djnago","10":"tag-python","11":"tag-template","12":"tag-view","13":"entry"},"uagb_featured_image_src":{"full":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",709,480,false],"thumbnail":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480-150x150.png",150,150,true],"medium":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480-300x203.png",300,203,true],"medium_large":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",709,480,false],"large":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",709,480,false],"1536x1536":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",709,480,false],"2048x2048":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",709,480,false],"home-middle":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",517,350,false],"home-top":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",620,420,false],"sidebar-thumbnail":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2018\/12\/christmas-2975401__480.png",100,68,false]},"uagb_author_info":{"display_name":"9nmatsumoto","author_link":"https:\/\/www.nblog09.com\/w\/author\/9nmatsumoto\/"},"uagb_comment_info":0,"uagb_excerpt":"&nbsp; \u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3067\u30d3\u30e5\u30fc\u304b\u3089\u6e21\u3055\u308c\u305f\u5909\u6570\u3092\u8868\u793a\u3059\u308b \u30ea\u30b9\u30c8\u578b\u306e\u5909\u6570\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u6307\u5b9a\u3057\u3066\u4e00\u90e8\u3092\u8868\u793a&hellip;","_links":{"self":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/686","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=686"}],"version-history":[{"count":17,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/686\/revisions"}],"predecessor-version":[{"id":3781,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/686\/revisions\/3781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media\/354"}],"wp:attachment":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media?parent=686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/categories?post=686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/tags?post=686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}