{"id":982,"date":"2019-05-04T17:43:15","date_gmt":"2019-05-04T08:43:15","guid":{"rendered":"https:\/\/www.nblog09.com\/w\/?p=982"},"modified":"2021-11-11T17:41:23","modified_gmt":"2021-11-11T08:41:23","slug":"django_template_view","status":"publish","type":"post","link":"https:\/\/www.nblog09.com\/w\/2019\/05\/04\/django_template_view\/","title":{"rendered":"Django\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d3\u30e5\u30fc\uff08TemplateView\uff09\u306e\u5229\u7528\u65b9\u6cd5\u307e\u3068\u3081"},"content":{"rendered":"<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d3\u30e5\u30fc(Template View)\u306e\u5229\u7528\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8a73\u8ff0\u3059\u308b\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2 class=\"title-border\"><strong><\/p>\n<p>TemplateView\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9<\/p>\n<p><\/strong><\/h2>\n<h3><b><\/p>\n<p>1.TemplateView\u3092\u7528\u3044\u305f\u753b\u9762\u8868\u793a<br \/>\n<\/b><br \/>\n<\/h3>\n<p>TemplateView\u3092\u4f7f\u7528\u3057\u3066\u3001\u753b\u9762\u3092\u8868\u793a\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u307e\u305a\u306f\u8a18\u8f09\u3057\u3066\u3044\u304f\u3002<\/p>\n<p>view\u306b\u306f\u7c21\u5358\u306b\u3001view_name=''\u3068\u3057\u3066\u3001\u8868\u793a\u3059\u308b\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u30d1\u30b9\u3092\u8a2d\u5b9a\u3059\u308b\u3002<br \/>\n(\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u30d1\u30b9\u306e\u8a2d\u5b9a\u65b9\u6cd5\u306f<a href=\"https:\/\/www.nblog09.com\/w\/2019\/01\/20\/djngo-template\/\">\u3053\u3061\u3089<\/a>\u3092\u53c2\u7167)<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>views.py(\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u30d1\u30b9\u306e\u8a2d\u5b9a)<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django.views.generic import TemplateView<\/p>\n<p>class IndexView(TemplateView):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"codehighlight\">template_name = 'index.html'<\/span><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<strong># template_name\u306b\u8868\u793a\u3059\u308b\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3092\u6307\u5b9a<\/strong>\n<\/div>\n<p>\u6b21\u306b\u3001urls.py\u306e\u8a2d\u5b9a\u3092\u884c\u3046\u3002<br \/>\nurls.py\u306e\u8a2d\u5b9a\u306f\u30b7\u30f3\u30d7\u30eb\u306b\u30af\u30e9\u30b9\u540d.as_view()\u3068\u3059\u308b\u3002<\/p>\n<ul>\n<li>urls.py<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django.contrib import admin<br \/>\nfrom django.urls import path<br \/>\nfrom basic_app import views<\/p>\n<p>urlpatterns = [<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;path('admin\/', admin.site.urls),<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;path('', views.IndexView<span class=\"codehighlight\">.as_view()<\/span>, name='index')<br \/>\n]\n<\/div>\n<p>\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d5\u30a1\u30a4\u30eb\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u305f\u3002<\/p>\n<ul>\n<li>index.html<\/li>\n<\/ul>\n<div class=\"programming-inside\">\n{% extends \"base.html\" %}<br \/>\n{% block body_block %}<br \/>\n    &lt;h1&gt;Hello World! TemplateView&lt;\/h1&gt;<br \/>\n{% endblock %}<\/div>\n<p>Django\u3092\u7acb\u3061\u4e0a\u3052\u3066\u753b\u9762\u3092\u8868\u793a\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8868\u793a\u3055\u308c\u308b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-983 aligncenter\" src=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/05\/template_view1.png\" alt=\"\" width=\"357\" height=\"219\" srcset=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/05\/template_view1.png 804w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/05\/template_view1-300x184.png 300w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/05\/template_view1-768x471.png 768w\" sizes=\"auto, (max-width: 357px) 100vw, 357px\" \/><\/p>\n<\/div>\n<h3><b>2.TemplateView\u3067\u306e\u5024\u306e\u5f15\u304d\u6e21\u3057\uff08get_context_data\uff09<\/b><\/h3>\n<p>TemplateView\u3067\u3001\u5024\u3092\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u6e21\u3057\u3066\u3001\u753b\u9762\u4e0a\u306b\u8868\u793a\u3057\u305f\u3044\u3053\u3068\u304c\u3042\u308b\u3002<\/p>\n<p>\u3053\u306e\u3088\u3046\u306a\u5834\u5408\u3001<span class=\"codehighlight\">\u4e00\u822c\u7684\u306bget_context_data\u95a2\u6570\u3092\u5229\u7528\u3059\u308b\u3002<\/span><\/p>\n<p>get_context_data\u306fTemplateView\u306e\u7d99\u627f\u5143django.views.generic.base.ContextMixin\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3001\u3053\u308c\u3092\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3057\u3066\u3001\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u5b9f\u884c\u3059\u308b\u524d\u306b\u547c\u3073\u51fa\u3055\u308c\u308b\u3088\u3046\u306b\u3057\u3001\u5909\u6570\u306b\u5024\u3092\u8a2d\u5b9a\u3059\u308b\u3002<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>views.py<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django.views.generic import TemplateView<\/p>\n<p>class IndexView(TemplateView):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;template_name = 'index.html'<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def <span class=\"codehighlight\">get_context_data(self, **kwargs):<\/span><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;context = super().get_context_data(**kwargs)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong># \u89aa\u306eget_context_data\u3092\u547c\u3093\u3067\u5024\u3092\u53d6\u5f97<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;context['injectme'] = 'Basic Injection'<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return context<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong># \u65b0\u305f\u306b\u5024\u3092\u8ffd\u52a0\u3057\u3066\u5024\u3092\u8fd4\u3059<\/strong>\n<\/div>\n<p>urls.py\u306f\u5909\u66f4\u305b\u305a\u3001index.html\u306e\u8a2d\u5b9a\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u5024\u3092\u53d6\u5f97\u3057\u305f\u3002<\/p>\n<ul>\n<li>index.html<\/li>\n<\/ul>\n<div class=\"programming-inside\">\n{% extends \"base.html\" %}<br \/>\n{% block body_block %}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;h1&gt;Hello World! TemplateView&lt;\/h1&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2&gt;Injected Content: {{ injectme }}&lt;\/h2&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;!-- views.py\u3067\u8a2d\u5b9a\u3057\u305f\u5024\u3092\u8868\u793a --L<br \/>\n{% endblock %}\n<\/div>\n<p>Django\u3092\u52d5\u304b\u3057\u3066\u3001\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u753b\u9762\u304c\u8868\u793a\u3055\u308c\u308b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-984 aligncenter\" src=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/05\/template_view2.png\" alt=\"\" width=\"372\" height=\"332\" srcset=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/05\/template_view2.png 792w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/05\/template_view2-300x268.png 300w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/05\/template_view2-768x686.png 768w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/>\n<\/div>\n<div class=\"django-adsense\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d3\u30e5\u30fc(Template View)\u306e\u5229\u7528\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8a73\u8ff0\u3059\u308b\u3002 &nbsp; TemplateView\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9 1.TemplateView\u3092\u7528\u3044\u305f\u753b\u9762\u8868\u793a TemplateView\u3092 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":756,"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],"tags":[122,14,164],"class_list":{"0":"post-982","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-django","8":"tag-django","9":"tag-python","10":"tag-templateview","11":"entry"},"uagb_featured_image_src":{"full":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/02\/system-2660914__480.jpg",720,480,false],"thumbnail":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/02\/system-2660914__480-150x150.jpg",150,150,true],"medium":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/02\/system-2660914__480-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/02\/system-2660914__480.jpg",720,480,false],"large":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/02\/system-2660914__480.jpg",720,480,false],"1536x1536":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/02\/system-2660914__480.jpg",720,480,false],"2048x2048":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/02\/system-2660914__480.jpg",720,480,false],"home-middle":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/02\/system-2660914__480.jpg",525,350,false],"home-top":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/02\/system-2660914__480.jpg",630,420,false],"sidebar-thumbnail":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/02\/system-2660914__480.jpg",100,67,false]},"uagb_author_info":{"display_name":"9nmatsumoto","author_link":"https:\/\/www.nblog09.com\/w\/author\/9nmatsumoto\/"},"uagb_comment_info":1,"uagb_excerpt":"\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d3\u30e5\u30fc(Template View)\u306e\u5229\u7528\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8a73\u8ff0\u3059\u308b\u3002 &nbsp; Te&hellip;","_links":{"self":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/982","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=982"}],"version-history":[{"count":11,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/982\/revisions"}],"predecessor-version":[{"id":3800,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/982\/revisions\/3800"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media\/756"}],"wp:attachment":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media?parent=982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/categories?post=982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/tags?post=982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}