{"id":296,"date":"2019-10-31T00:37:43","date_gmt":"2019-10-30T16:37:43","guid":{"rendered":"https:\/\/hophd.wordpress.com\/?p=296"},"modified":"2019-10-31T00:37:43","modified_gmt":"2019-10-30T16:37:43","slug":"django-for-ajax","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2019\/10\/31\/django-for-ajax\/","title":{"rendered":"\u57fa\u672cDjango\u7db2\u7ad9\u67b6\u7ad9\u6b65\u9a5f"},"content":{"rendered":"\n<p>\u5047\u8a2d\u6211\u5011\u60f3\u8981\u7c21\u55ae\u5730\u67b6\u8a2d\u4e00\u500b\u7db2\u9801\u4f3a\u670d\u5668\u63d0\u4f9bajax\u6240\u9700\u8981\u4f3a\u670d\u5668\u7aef\u8cc7\u6599\uff0c\u800c\u9019\u500b\u4f3a\u670d\u5668\u4f7f\u7528\u7684\u662fPython\/Django\/SQLite\u67b6\u69cb\u7684\u8a71\uff0c\u8acb\u4f9d\u7167\u672c\u7bc7\u6587\u7ae0\u7684\u8aaa\u660e\u9010\u6b65\u64cd\u4f5c\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u8acb\u5148\u5728\u4f60\u7684\u96fb\u8166\u4e2d\u5b89\u88dd<a href=\"https:\/\/www.anaconda.com\/distribution\/\">Anaconda<\/a>\u3002<\/p>\n\n\n\n<p>\u5b89\u88dd\u5b8c\u7562\u4e4b\u5f8c\uff0c\u8acb\u5230\u7a0b\u5f0f\u96c6\u4e2d\u53bb\u57f7\u884cAnaconda Prompt\uff0c\u9032\u5165\u5177\u5099Anaconda Python\u74b0\u5883\u7684\u547d\u4ee4\u63d0\u793a\u5b57\u5143\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fMacOS\u7684\u8a71\uff0c\u53ea\u8981\u9032\u5165\u7d42\u7aef\u6a5f\u4ecb\u9762\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<p>\u70ba\u4e86\u53ef\u4ee5\u8b93\u6e2c\u8a66\u7684\u74b0\u5883\u5118\u91cf\u5730\u4e0d\u5f71\u97ff\u5230\u5176\u5b83\u7684\u5c08\u6848\uff0c\u5728\u63a5\u4e0b\u4f86\u7684\u5b89\u88dd\u52d5\u4f5c\u4e4b\u524d\uff0c\u8acb\u5148\u5728\u4f60\u7684Anaconda Prompt\u74b0\u5883\u4e2d\u5b89\u88dd\u865b\u64ec\u74b0\u5883\u3002\u4f55\u8001\u5e2b\u6bd4\u8f03\u7fd2\u6163\u4f7f\u7528\u7684\u865b\u64ec\u74b0\u5883\u662fvirtualenv\u3002<\/p>\n\n\n\n<p>\u4e4b\u5f8c\uff0c\u8acb\u9078\u64c7\u4efb\u4e00\u500b\u8cc7\u6599\u593e\uff0c\u5728\u8a72\u8cc7\u6599\u593e\u4e2d\u5efa\u7acb\u4e00\u500b\u6e2c\u8a66\u7528\u7684\u5c08\u5c6c\u8cc7\u6599\u593e\uff0c\u5728\u6b64\u6211\u5011\u4ee5ajaxtest\u70ba\u4f8b\uff0c\u5728\u6b64\u8cc7\u6599\u593e\u4e4b\u4e0b\u5229\u7528\u4ee5\u4e0b\u7684\u6307\u4ee4\u5275\u5efa\u51fa\u4e00\u500b\u865b\u64ec\u74b0\u5883\u7528\u7684\u8cc7\u6599\u593e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>virtualenv venv<\/code><\/pre>\n\n\n\n<p>\u57f7\u884c\u4ee5\u4e0b\u6307\u4ee4\u4ee5\u555f\u7528\u865b\u64ec\u74b0\u5883\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>venv\\Scripts\\activate<\/code><\/pre>\n\n\n\n<p>\u57f7\u884c\u904e\u7a0b\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2019\/10\/image-17.png?w=723\" alt=\"\" class=\"wp-image-300\" width=\"683\" height=\"327\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-17.png 723w, https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-17-300x144.png 300w\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" \/><\/figure>\n\n\n\n<p>\u5728\u6b64\u74b0\u5883\u4e4b\u4e0b\uff0c\u5373\u53ef\u5229\u7528pip\u5b89\u88dddjango\uff0c\u6307\u4ee4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install django<\/code><\/pre>\n\n\n\n<p>\u5b89\u88dd\u7684\u904e\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2019\/10\/image-19.png?w=723\" alt=\"\" class=\"wp-image-303\" width=\"672\" height=\"322\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-19.png 723w, https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-19-300x144.png 300w\" sizes=\"auto, (max-width: 672px) 100vw, 672px\" \/><\/figure>\n\n\n\n<p>\u5982\u679c\u6c92\u6709\u4efb\u4f55\u7684\u932f\u8aa4\u8a0a\u606f\u51fa\u73fe\u6642\u5373\u53ef\u958b\u59cb\u5efa\u7acb\u7db2\u7ad9\uff0c\u6307\u4ee4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>django-admin startproject datacenter\ncd datacenter\npython manage.py startapp mysite<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff03\u500b\u6307\u4ee4\u6703\u5efa\u7acb\u51fa\u4e00\u500b\u53eb\u505adatacenter\u7684\u7db2\u7ad9\uff0c\u4e26\u5728\u8a72\u7db2\u7ad9\u4e4b\u4e0b\u5efa\u7acb\u51fa\u4e00\u500bapp\u3002\u5176\u4e2d\uff0cdatacenter\\datacenter\\settings.py\u662f\u8a2d\u5b9a\u7db2\u7ad9\u76f8\u95dc\u8cc7\u6599\u7684\u5730\u65b9\uff0c\u9700\u5229\u7528\u7a0b\u5f0f\u78bc\u7de8\u8f2f\u5668\u4fee\u6539\u5fc5\u8981\u7684\u8a2d\u5b9a\uff08\u8acb\u53c3\u8003<a href=\"https:\/\/www.books.com.tw\/products\/0010790747\">\u66f8\u672c\u4e0a<\/a>\u6216\u662f\u6295\u5f71\u7247\u4e0a\u7684\u8aaa\u660e\uff09\u3002<\/p>\n\n\n\n<p>\u5b8c\u6210\u5fc5\u8981\u8a2d\u5b9a\u7684\u4fee\u6539\u4e4b\u5f8c\uff0c\u5373\u53ef\u5229\u7528\u4ee5\u4e0b\u7684\u6307\u4ee4\u9032\u884c\u8cc7\u6599\u5eab\u521d\u59cb\u5316\u4f5c\u696d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python manage.py makemigrations\npython manage.py migrate<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u5169\u500b\u6307\u4ee4\u7684\u57f7\u884c\u904e\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2019\/10\/image-20.png?w=723\" alt=\"\" class=\"wp-image-307\" width=\"672\" height=\"453\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-20.png 723w, https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-20-300x202.png 300w\" sizes=\"auto, (max-width: 672px) 100vw, 672px\" \/><\/figure>\n\n\n\n<p>\u5728\u6c92\u6709\u51fa\u73fe\u932f\u8aa4\u8a0a\u606f\u7684\u60c5\u6cc1\u4e0b\uff0c\u8868\u793a\u6211\u5011\u7684\u7db2\u7ad9\u6846\u67b6\u5df2\u7d93\u521d\u59cb\u5316\u5b8c\u6210\u4e86\uff0c\u53ef\u4ee5\u5229\u7528\u4ee5\u4e0b\u7684\u6307\u4ee4\u9032\u884c\u6e2c\u8a66\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python manage.py runserver<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u6307\u4ee4\u7684\u57f7\u884c\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2019\/10\/image-21.png?w=723\" alt=\"\" class=\"wp-image-308\" width=\"666\" height=\"449\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-21.png 723w, https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-21-300x202.png 300w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/figure>\n\n\n\n<p>\u9019\u662f\u4f3a\u670d\u5668\u5f8c\u53f0\u7684\u57f7\u884c\u8a0a\u606f\uff0c\u5b83\u6703\u628a\u4f3a\u670d\u5668\u958b\u555f\u5728127.0.0.1:8000\u7684\u7db2\u5740\u4e0a\uff0c\u4f7f\u7528\u700f\u89bd\u5668\u5728\u7db2\u5740\u5217\u8f38\u5165\u9019\u500b\u7db2\u5740\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u7db2\u9801\u4f3a\u670d\u5668\u904b\u4f5c\u7684\u6a23\u5b50\u4e86\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2019\/10\/image-22.png?w=948\" alt=\"\" class=\"wp-image-309\" width=\"668\" height=\"513\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-22.png 948w, https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-22-300x230.png 300w, https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-22-768x590.png 768w\" sizes=\"auto, (max-width: 668px) 100vw, 668px\" \/><\/figure>\n\n\n\n<p>\u4fee\u6539urls.py\uff0c\u52a0\u51652\u500b\u7db2\u9801\u9023\u7d50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.contrib import admin\nfrom django.urls import path\nfrom mysite import views\n\nurlpatterns = &#091;\n    path('admin\/', admin.site.urls),\n    path('date\/', views.date),\n    path('', views.index),\n]<\/code><\/pre>\n\n\n\n<p>\u4fee\u6539views.py\uff0c\u52a0\u5165\u5be6\u969b\u7522\u751f\u8cc7\u6599\u7684\u51fd\u6578\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from django.shortcuts import render\nfrom django.http import HttpResponse\nfrom django.http import JsonResponse\nimport datetime\ndef index(request):\n    return HttpResponse(\"&lt;h2&gt;\u6b61\u8fce\u4f86\u5230\u6211\u7684\u7db2\u7ad9&lt;\/h2&gt;\")\n\ndef date(request):\n    return JsonResponse({'date':datetime.datetime.now()})<\/code><\/pre>\n\n\n\n<p>\u6b64\u6642\u900f\u904e\u700f\u89bd\u5668\u700f\u89bd\u9996\u9801\u6703\u770b\u5230\u4ee5\u4e0b\u7684\u5167\u5bb9\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2019\/10\/image-23.png?w=713\" alt=\"\" class=\"wp-image-311\" width=\"665\" height=\"314\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-23.png 713w, https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-23-300x142.png 300w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/figure>\n\n\n\n<p>\u700f\u89bd127.0.0.1:8000\/date\uff0c\u5247\u53ef\u4ee5\u770b\u5230\u4ee5\u4e0b\u7684JSON\u8cc7\u6599\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2019\/10\/image-24.png?w=713\" alt=\"\" class=\"wp-image-312\" width=\"669\" height=\"316\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-24.png 713w, https:\/\/104.es\/wp-content\/uploads\/2019\/10\/image-24-300x142.png 300w\" sizes=\"auto, (max-width: 669px) 100vw, 669px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5047\u8a2d\u6211\u5011\u60f3\u8981\u7c21\u55ae\u5730\u67b6\u8a2d\u4e00\u500b\u7db2\u9801\u4f3a\u670d\u5668\u63d0\u4f9bajax\u6240\u9700\u8981\u4f3a\u670d\u5668\u7aef\u8cc7\u6599\uff0c\u800c\u9019\u500b\u4f3a\u670d\u5668\u4f7f\u7528\u7684\u662fPython\/Django\/SQLite\u67b6\u69cb\u7684\u8a71\uff0c\u8acb\u4f9d\u7167\u672c\u7bc7\u6587\u7ae0\u7684\u8aaa\u660e\u9010\u6b65\u64cd\u4f5c\u5373\u53ef\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6,15],"tags":[],"class_list":["post-296","post","type-post","status-publish","format-standard","hentry","category-django","category-python"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=296"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/296\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}