{"id":2070,"date":"2023-04-01T21:40:45","date_gmt":"2023-04-01T13:40:45","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2070"},"modified":"2023-04-06T16:01:41","modified_gmt":"2023-04-06T08:01:41","slug":"the-difference-between-django-httpresponse-and-jsonresponse","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/the-difference-between-django-httpresponse-and-jsonresponse\/","title":{"rendered":"Django HttpResponse\u4e0eJsonResponse\u533a\u522b"},"content":{"rendered":"<h2>\u4e24\u8005\u533a\u522b<\/h2>\n<ul>\n<li>HttpResponse Header: <code>content-type=application\/text<\/code><\/li>\n<li>JasonResponse Header: <code>content-type=application\/json<\/code><\/li>\n<\/ul>\n<p>HttpResponse\u7684json\u6570\u636e\u9700\u8981\u524d\u540e\u7aef\u8fdb\u884c\u5e8f\u5217\u5316\u4e0e\u53cd\u5e8f\u5217\u5316<\/p>\n<p><!-- more --><\/p>\n<h2>\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>ps\uff1a\u540e\u53f0\u8fd4\u56de\u7684\u6570\u636e\u90fd\u9700\u8981\u6709\u56fa\u5b9a\u7684\u683c\u5f0f\uff0c\u5305\u542b\u72b6\u6001\u7801\u4ee5\u53ca\u4fe1\u606f\uff0c\u5982<\/p>\n<pre><code class=\"language-python\">res = {&quot;code&quot;:None, &quot;msg&quot;:None}<\/code><\/pre>\n<h3>HttpResponse<\/h3>\n<p>\u540e\u7aef\u4ee3\u7801<\/p>\n<pre><code class=\"language-python\"># \u5148\u5b9a\u4e49\u51fa\u8fd4\u56de\u6570\u636e\u7684\u683c\u5f0f\nres = {&quot;code&quot;: None, &quot;data&quot;: None}\n# \u6dfb\u52a0\u8fd4\u56de\u7684\u6570\u636e\nres[&quot;code&quot;] = 10000\nres[&#039;data&#039;] = &quot;success&quot;\n# \u8fd4\u56de\nreturn HttpResponse(json.dumps(res))<\/code><\/pre>\n<p>\u524d\u7aef\u4ee3\u7801<\/p>\n<pre><code class=\"language-javascript\">\/\/js\u53cd\u5e8f\u5217\u5316:\nres = JSON.parse(data);\n\/\/js\u5e8f\u5217\u5316\nJSON.stringify(res)<\/code><\/pre>\n<h3>JasonResponse<\/h3>\n<p>\u540e\u7aef\u4ee3\u7801<\/p>\n<pre><code class=\"language-python\"># \u5148\u5b9a\u4e49\u51fa\u8fd4\u56de\u6570\u636e\u7684\u683c\u5f0f\nres = {&quot;code&quot;: None, &quot;data&quot;: None}\n# \u6dfb\u52a0\u8fd4\u56de\u7684\u6570\u636e\nres[&quot;code&quot;] = 10000\nres[&#039;data&#039;] = &quot;success&quot;\n# \u8fd4\u56de\nreturn JsonResponse(res)<\/code><\/pre>\n<p>\u524d\u7aef\u4ee3\u7801<\/p>\n<p>\u8fd4\u56de\u503c\u5c31\u662f\u5bf9\u5e94\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u901a\u8fc7\u5b57\u6bb5\u540d\u76f4\u63a5\u8bfb\u53d6\uff0c\u4e0d\u9700\u8981\u8fc7\u591a\u64cd\u4f5c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e24\u8005\u533a\u522b HttpResponse Header: content-type=application\/text [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[522],"class_list":["post-2070","post","type-post","status-publish","format-standard","hentry","category-python","tag-django"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2070","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2070"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2070\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}