{"id":307,"date":"2021-11-09T19:38:13","date_gmt":"2021-11-09T11:38:13","guid":{"rendered":"http:\/\/47.98.165.52\/?p=307"},"modified":"2021-11-09T19:38:13","modified_gmt":"2021-11-09T11:38:13","slug":"django-1-%e7%8e%af%e5%a2%83%e9%85%8d%e7%bd%ae%e5%8f%8a%e8%bf%9c%e7%a8%8b%e8%bf%9e%e6%8e%a5%e5%90%af%e5%8a%a8%e9%a1%b9%e7%9b%ae","status":"publish","type":"post","link":"\/?p=307","title":{"rendered":"Django-1.\u73af\u5883\u914d\u7f6e\u53ca\u8fdc\u7a0b\u8fde\u63a5\u542f\u52a8\u9879\u76ee"},"content":{"rendered":"<h1>1.\u521b\u5efa\u865a\u62df\u73af\u5883\u53ca\u5b89\u88c5Django<\/h1>\n<ul>\n<li>\u67e5\u770b\u5f53\u524d\u54ea\u4e9b\u865a\u62df\u73af\u5883\uff1a workon<\/li>\n<li>\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a  mkvirtualenv -p \/usr\/bin\/python3 django_lee<\/li>\n<li>\u8fdb\u5165\u865a\u62df\u73af\u5883\uff1a workon django_le<\/li>\n<li>\u9000\u51fa\u865a\u62df\u73af\u5883\uff1a  deactivate<\/li>\n<li>\u5220\u9664\u865a\u62df\u73af\u5883\uff1a   rmvirtualenv django_le<\/li>\n<li>\u5b89\u88c5Django \uff1a pip install django==2.1.7 -i <a href=\"https:\/\/pypi.douban.com\/simple\">https:\/\/pypi.douban.com\/simple<\/a><\/li>\n<li>\u67e5\u770b\u5f53\u524dpython\u73af\u5883\u4e0b\u7b2c\u4e09\u65b9\u5e93\uff1a pip list<\/li>\n<\/ul>\n<h1>2.\u65b0\u5efa\u9879\u76ee<\/h1>\n<pre><code>django-admin startproject projectname<\/code><\/pre>\n<h1>3.pycharm\u8fde\u63a5\u9879\u76ee<\/h1>\n<h2>(1)\u89e3\u91ca\u5668\u9009\u62e9<\/h2>\n<p><strong>\u89e3\u91ca\u5668\u9009\u62e9\u5f53\u524d\u9879\u76ee-\u865a\u62df\u73af\u5883\u4e2d\u7684python\u89e3\u91ca\u5668<\/strong><\/p>\n<ul>\n<li>\u65e0\u987b\u52fe\u9009sudo<br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/6d7f5b1572134d189f89d8c7b14462d6.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/a588f589104f464b8da8d369d0cf772b.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/li>\n<\/ul>\n<h2>\uff082\uff09\u540c\u6b65\u6587\u4ef6\u5939\u90e8\u7f72<\/h2>\n<p><img src=\"https:\/\/img-blog.csdnimg.cn\/00133bce12e5424fa69031df8a1a0429.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/b986ded1924f49d6b11a82c99a981c03.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/4a53728855c042f29b3b84c95280f306.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>\uff083\uff09\u4e0a\u4f20\u65b9\u5f0f\u914d\u7f6e<\/h2>\n<p><img src=\"https:\/\/img-blog.csdnimg.cn\/1eb57f432ca14a6bab8f36baa0a7ee76.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/bd5a63c11660482bbb7e9e35449132df.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h1>4.Django\u9879\u76ee\u76ee\u5f55\u53ca\u6587\u4ef6\u8bf4\u660e<\/h1>\n<p><img src=\"https:\/\/img-blog.csdnimg.cn\/053aa05ad7c54297bd4106498dcbac29.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h1>5.\u542f\u52a8\u9879\u76ee<\/h1>\n<h2>\uff081\uff09\u5f00\u53d1\u670d\u52a1\u5668<\/h2>\n<p><img src=\"https:\/\/img-blog.csdnimg.cn\/7d379aedb9354127a756ee1cc95f9def.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>1.\u5c06setting.py\u4e2d\u7684ALLOWED_HOSTS = []\u6539\u6210ALLOWED_HOSTS = ['*']<\/h3>\n<ul>\n<li>\u4ee3\u8868\u4efb\u610fIP\u90fd\u53ef\u4ee5\u5bf9Django\u9879\u76ee\u8fdb\u884c\u8bbf\u95ee<br \/>\n<h3>2.\u5f00\u542f\u670d\u52a1\u7684\u547d\u4ee4\uff1apython manage.py runserver  0.0.0.0:8000\/\u505c\u6b62\uff1aCtrl+c<\/h3>\n<p>&lt;-\u6d4f\u89c8\u5668\u5373\u53ef\u8bbf\u95ee-&gt;<br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/b1f356b5e1114e6b97c6a4df7bef901a.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/6acba8aa957a482fa96fcc83a23b38c6.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<\/li>\n<\/ul>\n<h3>3.pycharm\u542f\u52a8\u670d\u52a1\u914d\u7f6e<\/h3>\n<ul>\n<li><strong>\u53f3\u4e0a\u89d2\u627e\u5230\u914d\u7f6e\uff1a<\/strong><\/li>\n<\/ul>\n<p><img src=\"https:\/\/img-blog.csdnimg.cn\/3c418372f0cd417dad472c5835d86ee7.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<ul>\n<li><strong>\u70b9\u51fb\u4fee\u590d\uff1a<\/strong><\/li>\n<\/ul>\n<p><img src=\"https:\/\/img-blog.csdnimg.cn\/0c070f5600284028acb42d3dd9779128.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<ul>\n<li><strong>\u5373\u53ef\u542f\u52a8\u670d\u52a1<\/strong><\/li>\n<\/ul>\n<p><img src=\"https:\/\/img-blog.csdnimg.cn\/0930c4b4ccd342fd9abfdb382e3a21d8.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>\uff082\uff09\u521b\u5efa\u89c6\u56fe\u51fd\u6570<\/h2>\n<h3>1.\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u521b\u5efaviews.py\u7684\u51fd\u6570<\/h3>\n<pre><code>####################\n####  views.py  ####\n####################\nfrom django.http import HttpResponse\n\ndef index(request):#request\u4f1a\u5c06\u8bf7\u6c42\u4f53\u4f20\u53c2\n    return HttpResponse('hello django')<\/code><\/pre>\n<h3>2.\u5b9a\u4e49\u89c6\u56fe\u5bf9\u5e94\u7684URL<\/h3>\n<pre><code>####################\n####  urls.py  ####\n####################\nfrom django.contrib import admin\nfrom django.urls import path\nfrom .views import index\n\nurlpatterns = [\n    path('admin\/', admin.site.urls),\n    path('index\/',index)\n]<\/code><\/pre>\n<ul>\n<li><strong>\u6d4f\u89c8\u5668\u6548\u679c\uff1a<\/strong><\/li>\n<\/ul>\n<p><img src=\"https:\/\/img-blog.csdnimg.cn\/d192391bb75e44fd8af7e4fb618ef44d.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>\uff083\uff09\u65b0\u5efaapp<\/h2>\n<h3>\u65b9\u5f0f\u4e00\uff1a\u5728\u7ec8\u7aef\u6267\u884c\u4e2d\u6267\u884c\u547d\u4ee4<\/h3>\n<p><strong>\u521b\u5efa\u547d\u4ee4\uff1apython manage.py startapp app_name<\/strong><\/p>\n<h4>1.\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u521b\u5efa\u6267\u884c\uff0c\u6267\u884c\u521b\u5efa\u547d\u4ee4<\/h4>\n<p>\u6ce8\u610f\uff1a\u9700\u5728\u9879\u76ee\u73af\u5883\u4e0b\uff0c\u5728\u9879\u76ee\u6587\u4ef6\u5939\u5185\u6267\u884c\u8be5\u547d\u4ee4<\/p>\n<pre><code>(django_lee) lemon@instance-06qq0w7c:~\/testPro$ ls\ndb.sqlite3  manage.py  testPro\n(django_lee) lemon@instance-06qq0w7c:~\/testPro$ python manage.py startapp book\n(django_lee) lemon@instance-06qq0w7c:~\/testPro$ ls\nbook  db.sqlite3  manage.py  testPro<\/code><\/pre>\n<h4>2.\u5728pycharm\u4e2d\u5c06\u521b\u5efa\u7684app\u4e0b\u8f7d\u5230\u672c\u5730<\/h4>\n<p><img src=\"https:\/\/img-blog.csdnimg.cn\/862ce4a10ae347f4bb84491c1f0e9c59.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>\u65b9\u5f0f\u4e8c\uff1a\u5728pycharm\u4e2d\u521b\u5efa<\/h3>\n<p>Tools =&gt; Run manage.py task..<br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/da040a6ccc1141cea3131102409162fa.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n\u7136\u540e\u518d\u8fdb\u884c\u4e0b\u8f7d\u64cd\u4f5c\uff0c\u53ef\u4ee5\u770b\u5230\uff0c\u521a\u521a\u521b\u5efa\u7684\u4e24\u4e2aapp\u6587\u4ef6\u5939\u5c31\u6709\u4e86<br \/>\n<img src=\"https:\/\/img-blog.csdnimg.cn\/66c46dd069a44ac89ea0c6cceff8ee4d.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xlbW9uZ3Vlc3M=,size_16,color_FFFFFF,t_70\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u521b\u5efa\u865a\u62df\u73af\u5883\u53ca\u5b89\u88c5Django \u67e5\u770b\u5f53\u524d\u54ea\u4e9b\u865a\u62df\u73af\u5883\uff1a workon \u521b\u5efa\u865a\u62df\u73af\u5883\uff1a mkvirtualenv -p \/usr\/bi   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[26],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/307"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=307"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/307\/revisions"}],"predecessor-version":[{"id":308,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/307\/revisions\/308"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}