{"id":834,"date":"2019-04-07T16:42:19","date_gmt":"2019-04-07T07:42:19","guid":{"rendered":"https:\/\/www.nblog09.com\/w\/?p=834"},"modified":"2021-11-11T17:37:28","modified_gmt":"2021-11-11T08:37:28","slug":"django-query","status":"publish","type":"post","link":"https:\/\/www.nblog09.com\/w\/2019\/04\/07\/django-query\/","title":{"rendered":"Django\u3067GET\u30ea\u30af\u30a8\u30b9\u30c8\u3067\u5024\u3092\u9001\u4fe1\u3057\u3066\u3001View\u5185\u3067\u51e6\u7406\u3057\u3066\u753b\u9762\u4e0a\u306b\u8868\u793a\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>\u4eca\u56de\u306f\u3001GET\u30ea\u30af\u30a8\u30b9\u30c8\u3067View\u306b\u5024\u3092\u9001\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8a18\u8ff0\u3059\u308b\u3002<\/p>\n<p>HTTP\u30e1\u30bd\u30c3\u30c9\u306eGET\u3067\u5024\u3092\u9001\u308b\u3002<\/p>\n<p>\u65b9\u6cd5\u306f\u4e3b\u306b2\u3064\u3042\u308b\u306e\u3067\u305d\u308c\u305e\u308c\u8a18\u8ff0\u3059\u308b<\/p>\n<ul>\n<li><a href=\"#article1\">var1=value1&amp;var2=value2\u3067\u9001\u308b\u65b9\u6cd5<\/a><\/li>\n<li><a href=\"#article2\">url\u30d1\u30bf\u30fc\u30f3\u30de\u30c3\u30c1\u30f3\u30b0\u3067\u5024\u3092\u9001\u308b\u65b9\u6cd5<\/a><\/li>\n<\/ul>\n<h2 class=\"title-border\"><strong><br \/>\n?var1=value1&amp;var2=value2\u3067\u9001\u308b\u65b9\u6cd5<\/strong><\/h2>\n<p>\u3053\u306e\u5834\u5408\u3001view\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306bGET\u30e1\u30bd\u30c3\u30c9\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u9001\u3089\u308c\u3066\u304d\u305f\u60f3\u5b9a\u3067View\u5074\u306e\u30b3\u30fc\u30c9\u3092\u4f5c\u6210\u3059\u308b<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>view.py\u306e\u8a18\u8ff0\u5185\u5bb9(request.GET\u304b\u3089\u53d6\u5f97)<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django.http import HttpResponse<\/p>\n<p>def index(request):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;msg = ''<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if 'msg' in request.GET: <strong># requst.GET\u5185\u306bmsg\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u78ba\u8a8d\u3059\u308b\u3002url\u306b?msg=\u25cb\u25cb\u3068\u3042\u308b\u3068\u304d\u306b\u3001msg\u306b\u5bfe\u5fdc\u3057\u305f\u25cb\u25cb\u306e\u5024\u3092\u53d6\u5f97\u3067\u304d\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg = <span class=\"codehighlight\">request.GET['msg']<\/span> <strong># msg=hoge\u306e\u5834\u5408\u3001hoge\u3092\u53d6\u5f97\u3057\u3066\u5909\u6570msg\u306b\u683c\u7d0d\u3059\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return HttpResponse(f'Your message: {msg}') <strong># Your message: {msg}\u3092\u753b\u9762\u306b\u8868\u793a<\/strong>\n<\/div>\n<ul>\n<li>\u3053\u308c\u3067\u3001\u4e0b\u306e\u3088\u3046\u306b?msg=hoge\u3068url\u3092\u6307\u5b9a\u3057\u3066\u30a2\u30af\u30bb\u30b9\u3057\u305f\u5834\u5408\u3001\u753b\u9762\u306bYour message: hoge\u3068\u8868\u793a\u3055\u308c\u308b<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-843 aligncenter\" src=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get1-300x165.png\" alt=\"\" width=\"404\" height=\"222\" srcset=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get1-300x165.png 300w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get1-768x422.png 768w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get1-1024x563.png 1024w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get1.png 1633w\" sizes=\"auto, (max-width: 404px) 100vw, 404px\" \/><\/p>\n<ul>\n<li>\u3082\u3057msg\u3092\u6307\u5b9a\u3057\u306a\u304b\u3063\u305f\u5834\u5408\u3001view\u5185\u3067msg\u306b\u5024\u304c\u683c\u7d0d\u3055\u308c\u305a\u753b\u9762\u306b\u306fYour message:\u306e\u307f\u8868\u793a\u3055\u308c\u308b\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-844 aligncenter\" src=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get2-300x113.png\" alt=\"\" width=\"404\" height=\"152\" srcset=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get2-300x113.png 300w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get2-768x288.png 768w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get2-1024x384.png 1024w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get2.png 1648w\" sizes=\"auto, (max-width: 404px) 100vw, 404px\" \/><\/p>\n<\/div>\n<p>\u307e\u305f\u3001GET\u3067\u6e21\u3055\u308c\u308b\u5024\u306f\u3001<span class=\"pink_span\">\u578b\u304c\u6587\u5b57\u5217\u578b(str)\u306b\u306a\u308b<\/span><\/p>\n<p>\u4f8b\u3048\u3070\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5834\u5408\u3001\u6587\u5b57\u5217\u3068\u6570\u5024\u306e\u8a08\u7b97\u306b\u306a\u308a\u3001\u8a08\u7b97\u304c\u3067\u304d\u306a\u3044\u305f\u3081\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>views.py<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django.http import HttpResponse<\/p>\n<p>def index(request):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num = ''<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if 'num' in request.GET:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num = request.GET['num'] <strong># request\u304b\u3089num=\u25cb\u25cb\u306e\u5024\u3092\u53d6\u5f97<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(type(num)) <strong># str<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum = num + 1 <strong># str + int\u3068\u306a\u308a\u30a8\u30e9\u30fc<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return HttpResponse(f'{num}  + 1 = {sum}')\n<\/div>\n<ul>\n<li>http:\/\/127.0.0.1:8000\/app1\/index\/?num=12\u3067\u30a2\u30af\u30bb\u30b9\u3057\u3001TypeError\u3068\u306a\u308b\u4f8b<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-845 aligncenter\" src=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get3-300x190.png\" alt=\"\" width=\"404\" height=\"256\" srcset=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get3-300x190.png 300w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get3-768x487.png 768w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get3-1024x650.png 1024w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get3.png 1377w\" sizes=\"auto, (max-width: 404px) 100vw, 404px\" \/><\/p>\n<p>\u3053\u306e\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4fee\u6b63\u3059\u308b\u3002<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>views.py(\u4fee\u6b63\u7248)<\/li>\n<\/ul>\n<div class=\"programming-inside\">from django.http import HttpResponse<\/p>\n<p>def index(request):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num = ''<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if 'num' in request.GET:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num = request.GET['num']<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(type(num)) <strong># str<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum = int(num) + 1 <strong># int + int\u3068\u306a\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return HttpResponse(f'{num}  + 1 = {sum}') <strong># num + 1 = sum\u3068\u753b\u9762\u306b\u8868\u793a\u3055\u308c\u308b<\/strong>\n<\/div>\n<ul>\n<li>\u4fee\u6b63\u5f8cnum=1\u3068\u3057\u3066\u30a2\u30af\u30bb\u30b9\u3057\u305f\u7d50\u679c<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-846 aligncenter\" src=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get4-300x136.png\" alt=\"\" width=\"382\" height=\"173\" srcset=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get4-300x136.png 300w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get4-768x348.png 768w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get4.png 1022w\" sizes=\"auto, (max-width: 382px) 100vw, 382px\" \/><\/p>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h2 class=\"title-border\"><strong><br \/>\nurl\u30d1\u30bf\u30fc\u30f3\u30de\u30c3\u30c1\u30f3\u30b0\u3067\u5024\u3092\u9001\u308b\u65b9\u6cd5(http:\/\/\u25cb\u25cb\u25cb\/\u25cb\u25cb\/\u00d7\u00d7\u3068\u3059\u308b)<\/strong><\/h2>\n<p>\u3053\u308c\u306f\u3001url\u306b?\u25cb\u25cb=\u25cb\u25cb\u3068\u66f8\u304b\u305a\u306b\u3001\/\u25cb\u25cb\/\u00d7\u00d7\u3068\u8a18\u8ff0\u3057\u3066GET\u30ea\u30af\u30a8\u30b9\u30c8\u3067\u9001\u308b\u3084\u308a\u65b9\u3092\u8a00\u3046\u3002<\/p>\n<p>\uff08<a href=\"https:\/\/www.nblog09.com\/w\/2019\/01\/20\/django-mapping\/\">URL\u30d1\u30bf\u30fc\u30f3\u30de\u30c3\u30c1\u30f3\u30b0\u306b\u3064\u3044\u3066\u8a73\u7d30<\/a>\uff09<\/p>\n<p>\u3053\u308c\u306b\u306f\u3001urls.py\u3068view.py\u305d\u308c\u305e\u308c\u4fee\u6b63\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>urls.py\u3067URL\u30d1\u30bf\u30fc\u30f3\u30de\u30c3\u30c1\u30f3\u30b0\u3092\u6307\u5b9a\u3059\u308b<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django.urls import path<br \/>\nfrom app1 import views<\/p>\n<p>app_name = 'app1'<\/p>\n<p>urlpatterns = [<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;path('<span class=\"codehighlight\">index\/&lt;str:msg&gt;\/&lt;int:num&gt;<\/span>', views.index, name='index'),<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<strong># GET\u3067\u9001\u4fe1\u3059\u308b\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066msg\u3068num\u3092\u6307\u5b9a\u3059\u308b\u3002\u3053\u306e\u5834\u5408\u3001msg\u306fstr\u3067\u3001num\u306fint\u3067\u6e21\u3055\u308c\u308b<\/strong><br \/>\n]\n<\/div>\n<p>\u6b21\u306b\u3001views.py\u3092\u4fee\u6b63\u3059\u308b<\/p>\n<ul>\n<li>views.py\u3067GET\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u53d6\u5f97<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django.http import HttpResponse<\/p>\n<p>def index(request, <span class=\"codehighlight\">msg, num<\/span>): <strong># GET\u3067\u53d6\u5f97\u3059\u308b\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066msg\u3068num\u3092\u6307\u5b9a<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;result = f\"{msg} = {num} years old\"<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return HttpResponse(result)\n<\/div>\n<p>\u3053\u308c\u3067\u4f8b\u3048\u3070<\/p>\n<p>http:\/\/127.0.0.1:8000\/app1\/index\/Taro\/12<\/p>\n<p>\u3068\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001\"Taro = 12 years old\"\u3068\u753b\u9762\u306b\u306f\u8868\u793a\u3055\u308c\u308b<\/p>\n<ul>\n<li>\u5b9f\u884c\u7d50\u679c<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-848 aligncenter\" src=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get5-300x210.png\" alt=\"\" width=\"370\" height=\"259\" srcset=\"https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get5-300x210.png 300w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get5-768x536.png 768w, https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/04\/get5.png 872w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/p>\n<\/div>\n<div class=\"django-adsense\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u56de\u306f\u3001GET\u30ea\u30af\u30a8\u30b9\u30c8\u3067View\u306b\u5024\u3092\u9001\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8a18\u8ff0\u3059\u308b\u3002 HTTP\u30e1\u30bd\u30c3\u30c9\u306eGET\u3067\u5024\u3092\u9001\u308b\u3002 \u65b9\u6cd5\u306f\u4e3b\u306b2\u3064\u3042\u308b\u306e\u3067\u305d\u308c\u305e\u308c\u8a18\u8ff0\u3059\u308b var1=value1&amp;var2=value2\u3067\u9001\u308b\u65b9\u6cd5 url\u30d1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"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":[],"class_list":{"0":"post-834","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-django","7":"category-it-programming","8":"entry","9":"has-post-thumbnail"},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"home-middle":false,"home-top":false,"sidebar-thumbnail":false},"uagb_author_info":{"display_name":"9nmatsumoto","author_link":"https:\/\/www.nblog09.com\/w\/author\/9nmatsumoto\/"},"uagb_comment_info":19,"uagb_excerpt":"\u4eca\u56de\u306f\u3001GET\u30ea\u30af\u30a8\u30b9\u30c8\u3067View\u306b\u5024\u3092\u9001\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8a18\u8ff0\u3059\u308b\u3002 HTTP\u30e1\u30bd\u30c3\u30c9\u306eGET\u3067\u5024\u3092\u9001\u308b\u3002 \u65b9\u6cd5&hellip;","_links":{"self":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/834","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=834"}],"version-history":[{"count":16,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/834\/revisions"}],"predecessor-version":[{"id":3793,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/834\/revisions\/3793"}],"wp:attachment":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media?parent=834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/categories?post=834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/tags?post=834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}