{"id":205438,"date":"2025-05-29T12:47:23","date_gmt":"2025-05-29T04:47:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205438\/"},"modified":"2025-05-29T12:47:23","modified_gmt":"2025-05-29T04:47:23","slug":"%e4%be%9b%e5%bc%80%e5%8f%91%e4%ba%ba%e5%91%98%e4%bd%bf%e7%94%a8%e7%9a%84-django-%e6%96%87%e4%bb%b6%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205438\/","title":{"rendered":"\u4f9b\u5f00\u53d1\u4eba\u5458\u4f7f\u7528\u7684 Django \u6587\u4ef6\u7ed3\u6784"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f9b\u5f00\u53d1\u4eba\u5458\u4f7f\u7528\u7684 Django \u6587\u4ef6\u7ed3\u6784<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u4f9b\u5f00\u53d1\u4eba\u5458\u4f7f\u7528\u7684 Django \u6587\u4ef6\u7ed3\u6784\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241202\/1733102308674d0ae41ab50.jpg\" class=\"aligncenter\"><\/p>\n<p>\u8fd9\u4efd django \u6587\u4ef6\u7ed3\u6784\u6307\u5357\u5c06\u5f15\u5bfc\u60a8\u4e86\u89e3 django \u9879\u76ee\u7684\u57fa\u672c\u5143\u7d20\u3002<\/p>\n<p><strong>\u5185\u5bb9<\/strong><\/p>\n<ol>\n<li>\u9879\u76ee\u6839\u76ee\u5f55<\/li>\n<li>\u9879\u76ee\u76ee\u5f55\uff08\u4f8b\u5982\uff0cyou_project_name\uff09<\/li>\n<li>\u5e94\u7528\u7a0b\u5e8f\uff08apps\uff09<\/li>\n<li>\u6a21\u677f\u76ee\u5f55<\/li>\n<li>\u9759\u6001\u76ee\u5f55<\/li>\n<li>\u5a92\u4f53\u76ee\u5f55<\/li>\n<li>\u865a\u62df\u73af\u5883 (venv\/)<\/li>\n<\/ol>\n<p><strong>1\u3002\u9879\u76ee\u6839\u76ee\u5f55<\/strong><\/p>\n<p>\u8be5\u76ee\u5f55\u5305\u542b\u6574\u4e2a django \u9879\u76ee\u3002\u5b83\u5305\u542b<\/p>\n<p><strong>-manage.py<\/strong>\uff1a\u5b83\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5b9e\u7528\u7a0b\u5e8f\uff0c\u5141\u8bb8\u6211\u4eec\u4e0e\u9879\u76ee\u4ea4\u4e92\u3002\u4e3b\u8981\u7528\u4e8e\u542f\u52a8\u5f00\u53d1\u670d\u52a1\u5668\u3001\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u3001\u8fd0\u884c\u8fc1\u79fb\u7b49<\/p>\n<p><strong>&#8211; \u9879\u76ee\u6587\u4ef6\u5939<\/strong>\uff08\u60a8\u7684\u9879\u76ee\u540d\u79f0\u6587\u4ef6\u5939\uff09\uff1a\u5b83\u5305\u542b\u6211\u4eec\u9879\u76ee\u7684\u8bbe\u7f6e\u548c\u914d\u7f6e\u3002<\/p>\n<p><strong>2\u3002\u9879\u76ee\u76ee\u5f55\uff08\u4f8b\u5982\uff0cyou_project_name\uff09<\/strong><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u6211\u4eec django \u9879\u76ee\u914d\u7f6e\u7684\u6587\u4ef6\u5939\u3002\u5b83\u5305\u62ec\u4ee5\u4e0b\u6587\u4ef6\uff1a<\/p>\n<p><strong>&#8211; init.py<\/strong>:<\/p>\n<p><strong>-settings.py<\/strong>\uff1a\u5305\u542b\u6211\u4eec\u9879\u76ee\u7684\u8bbe\u7f6e\uff0c\u4f8b\u5982\u914d\u7f6e\u3001\u6570\u636e\u5e93\u8bbe\u7f6e\u3001\u5b89\u88c5\u7684\u5e94\u7528\u7a0b\u5e8f\u3001\u5141\u8bb8\u7684\u4e3b\u673a\u3001\u4e2d\u95f4\u4ef6\u3002<\/p>\n<p><strong>&#8211; urls.py<\/strong>\uff1a\u5b83\u5305\u542b\u6211\u4eec\u9879\u76ee\u7684 url\uff08\u8def\u7531\u6211\u4eec\u89c6\u56fe\u7684\u8bf7\u6c42\uff09\u3002<\/p>\n<p><strong>&#8211; asgi.py<\/strong>: <\/p>\n<p><strong>&#8211; wsgi.py<\/strong>:<\/p>\n<p><strong>3\u3002\u5e94\u7528\u7a0b\u5e8f\uff08apps\uff09<\/strong>\uff1a<\/p>\n<p><strong>&#8211; models.py<\/strong>\uff1a\u5b83\u5305\u542b\u60a8\u9879\u76ee\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6216\u8005\u6211\u4eec\u53ef\u4ee5\u8bf4\u5e94\u7528\u7a0b\u5e8f\u7684\u6570\u636e\/\u6570\u636e\u5e93\u7ed3\u6784\u3002<\/p>\n<p><strong>-views.py<\/strong>\uff1a\u4e1a\u52a1\u903b\u8f91\uff08\u5904\u7406\u8bf7\u6c42\u548c\u54cd\u5e94\uff09<\/p>\n<p><strong>&#8211; urls.py<\/strong>\uff1a\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u7279\u5b9a\u7f51\u5740<\/p>\n<p><strong>&#8211; forms.py<\/strong>\uff1a\u8868\u5355\u7684\u7ed3\u6784\u548c\u9a8c\u8bc1\u903b\u8f91<\/p>\n<p><strong>&#8211; admin.py<\/strong>\uff1a\u901a\u8fc7\u6ce8\u518c\u6a21\u578b\u6765\u5b9e\u73b0 django \u7ba1\u7406\u9762\u677f\uff08\u4eea\u8868\u677f\uff09\uff08\u901a\u8fc7\u521b\u5efa\u8d85\u7ea7\u7528\u6237\u5e76\u767b\u5f55 django \u7ba1\u7406\u5458\uff09<\/p>\n<p><strong>&#8211; apps.py<\/strong>:<\/p>\n<p><strong>&#8211; migrations\/<\/strong>\uff1a\u5305\u542b\u6570\u636e\u5e93\u8fc1\u79fb\u6587\u4ef6\u3002\u6bcf\u6b21\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u4efb\u4f55\u66f4\u6539\u65f6\uff0c\u60a8\u90fd\u4f1a\u5728\u6b64\u6587\u4ef6\u5939\u4e2d\u770b\u5230\u4e00\u4e2a\u5e26\u6709\u4e00\u4e9b\u968f\u673a naes \u7684\u65b0\u6587\u4ef6\uff08\u4f8b\u5982 0001_initial\u30010002_model_you_made_or_changes\uff0c&#8230;\uff09 <\/p>\n<p><strong>4\u3002\u6a21\u677f\u76ee\u5f55<\/strong>:<\/p>\n<p><strong>&#8211; base.html<\/strong>\uff1a\u8fd9\u5305\u542b\u8bb8\u591a\u6587\u4ef6\u4e2d\u5e38\u89c1\u7684\u5171\u4eab\u4ee3\u7801\uff0c\u4f8b\u5982\u60a8\u60f3\u8981\u5728\u591a\u4e2a\u9875\u9762\u4e2d\u4f7f\u7528\u7684\u9875\u7709\u3001\u9875\u811a\u3002<\/p>\n<p>*<em>&#8211; \u4ece base.html \u6269\u5c55\u7528\u4e8e\u7279\u5b9a\u89c6\u56fe\u7684\u5176\u4ed6\u6587\u4ef6 *<\/em>\uff1a\u6bd4\u5982\u8bf4 login.html\u3001home.html \u7b49<\/p>\n<p><strong>5\u3002\u9759\u6001\u76ee\u5f55<\/strong>\uff1a\u5305\u542bcss\u3001javascript\u3001\u56fe\u50cf\u7b49\u9759\u6001\u6587\u4ef6\u3002\u5e94\u7528\u7a0b\u5e8f\u7279\u5b9a\u76ee\u5f55\u6216\u5168\u5c40\u76ee\u5f55\uff08\u6839\u636e\u60a8\u7684\u8981\u6c42\uff09\u3002<\/p>\n<p><strong>6\u3002\u5a92\u4f53\u76ee\u5f55<\/strong>\uff1a\u7528\u6237\u4e0a\u4f20\u7684\u6587\u4ef6\uff0c\u4f8b\u5982\u6587\u6863\uff0c\u4efb\u4f55\u5176\u4ed6\u6587\u4ef6\u53ef\u80fd\u662f\u7528\u6237\u7684\u4e2a\u4eba\u8d44\u6599\u56fe\u7247\u7b49<\/p>\n<p><strong>7\u3002\u865a\u62df\u73af\u5883(venv\/)<\/strong>\uff1a\u517b\u6210\u4e3a\u6bcf\u4e2adjango\u9879\u76ee\u521b\u5efa\u865a\u62df\u73af\u5883\u7684\u4e60\u60ef\uff0c\u4ee5\u9694\u79bb\u9879\u76ee\u4f9d\u8d56\u5173\u7cfb\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u5bf9\u4e8e\u9879\u76ee\u7279\u5b9a\u7684\u5305\u6765\u8bf4\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u5e76\u4e14\u4e0d\u4f1a\u5e72\u6270\u4efb\u4f55\u5168\u5c40\u73af\u5883\u3002<\/p>\n<p>\u60a8\u7684\u9879\u76ee\u540d\u79f0\/<br \/> \u2502<br \/> \u251c\u2500\u2500 \u7ba1\u7406.py<br \/> \u251c\u2500\u2500 \u4f60\u7684\u9879\u76ee\u540d\u79f0\/<br \/> \u2502 \u251c\u2500\u2500 <strong>init<\/strong>.py<br \/> \u2502 \u251c\u2500\u2500 settings.py<br \/> \u2502 \u251c\u2500\u2500 urls.py<br \/> \u2502 \u251c\u2500\u2500 wsgi.py<br \/> \u2502 \u2514\u2500\u2500 asgi.py<br \/> \u2502<br \/> \u251c\u2500\u2500 your_app_one\/<br \/> \u2502 \u251c\u2500\u2500 <strong>init<\/strong>.py<br \/> \u2502 \u251c\u2500\u2500 admin.py<br \/> \u2502 \u251c\u2500\u2500 apps.py<br \/> \u2502 \u251c\u2500\u2500 models.py<br \/> \u2502 \u251c\u2500\u2500views.py<br \/> \u2502 \u251c\u2500\u2500 urls.py<br \/> \u2502 \u2514\u2500\u2500 \u8fc1\u5f99\/<br \/> \u2502<br \/> \u251c\u2500\u2500 your_app_two\/<br \/> \u2502 \u251c\u2500\u2500 <strong>init<\/strong>.py<br \/> \u2502 \u251c\u2500\u2500 admin.py<br \/> \u2502 \u251c\u2500\u2500 apps.py<br \/> \u2502 \u251c\u2500\u2500 models.py<br \/> \u2502 \u251c\u2500\u2500views.py<br \/> \u2502 \u2514\u2500\u2500 \u8fc1\u5f99\/<br \/> \u2502<br \/> \u251c\u2500\u2500 \u6a21\u677f\/<br \/> \u2502 \u251c\u2500\u2500 base.html<br \/> \u2502 \u2514\u2500\u2500 home.html<br \/> \u2502<br \/> \u2514\u2500\u2500\u9759\u6001\/<br \/> \u251c\u2500\u2500 css\/<br \/> \u2514\u2500\u2500 js\/<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><br \/> \u5728\u5f00\u59cb\u4efb\u4f55\u8bed\u8a00\u7684\u4efb\u4f55\u9879\u76ee\u4e4b\u524d\u4e86\u89e3\u6587\u4ef6\u7ed3\u6784\u5bf9\u4e8e\u9ad8\u6548\u7684\u9879\u76ee\u5f00\u53d1\u90fd\u975e\u5e38\u91cd\u8981\u548c\u5fc5\u8981\u3002\u6211\u5e0c\u671b\u73b0\u5728\u5927\u5bb6\u53ef\u4ee5\u66f4\u8f7b\u677e\u5730\u5bfc\u822a\u548c\u7ba1\u7406\u4ee3\u7801\u5e93\u3002<\/p>\n<p>\u8bf7\u968f\u65f6\u8bc4\u8bba\u60a8\u7684\u60f3\u6cd5\u6216\u4efb\u4f55\u63d0\u793a\u3002<br \/><strong>\u5982\u679c\u60a8\u60f3\u5c06\u6240\u6709\u5fc5\u8981\u7684 django \u547d\u4ee4\u96c6\u4e2d\u5728\u4e00\u4e2a\u5730\u65b9\uff0c\u8bf7\u8bc4\u8bba<\/strong><\/p>\n<p><strong>\u5956\u91d1<\/strong><\/p>\n<p><strong>manage.py \u4e2d\u60a8\u5e94\u8be5\u4e86\u89e3\u7684\u547d\u4ee4<\/strong><\/p>\n<pre>    **1. python manage.py runserver ** : To start the server\n\n    **2. python manage.py makemigrations** : Creating new \n         migrations on the changes made in your models.\n\n    **3. python manage.py migrate ** : Applying or unapplying \n         migrations\n    **4. python manage.py createsuperuser**: Access to django \n         admin panel\n<\/pre>\n<ul>\n<li> <\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4f9b\u5f00\u53d1\u4eba\u5458\u4f7f\u7528\u7684 Django \u6587\u4ef6\u7ed3\u6784\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f9b\u5f00\u53d1\u4eba\u5458\u4f7f\u7528\u7684 Django &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205438","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205438"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205438\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}