{"id":3005,"date":"2016-11-27T09:40:31","date_gmt":"2016-11-27T01:40:31","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=3005"},"modified":"2016-11-27T09:40:31","modified_gmt":"2016-11-27T01:40:31","slug":"django%e7%9a%84%e4%bd%bf%e7%94%a8%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3005.html","title":{"rendered":"Django\u7684\u4f7f\u7528\u8bb0\u5f55"},"content":{"rendered":"<p>=Start=<\/p>\n<h4>\u7f18\u7531\uff1a<\/h4>\n<p>\u603b\u7ed3\u4ee5\u5f80\u7ecf\u9a8c\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003<\/p>\n<h4>\u6b63\u6587\uff1a<\/h4>\n<h5>1. \u7f16\u8bd1\u5b89\u88c5 Python 2.7<\/h5>\n<h5>2. \u5b89\u88c5 Python 2.7 \u5bf9\u5e94\u7684 PIP<\/h5>\n<h5>3. \u5229\u7528 PIP \u5b89\u88c5\u4e00\u4e9b\u6a21\u5757<\/h5>\n<pre class=\"lang:default decode:true\">$ pip install Django==1.8.16 MySQL-python virtualenv virtualenvwrapper<\/pre>\n<h5>4. \u4f7f VirtualENV \u7acb\u5373\u751f\u6548<\/h5>\n<pre class=\"lang:default decode:true\">$ vim ~\/.bashrc\nexport WORKON_HOME=$HOME\/.virtualenvs\nexport PROJECT_HOME=$HOME\/workspace\nsource \/usr\/local\/bin\/virtualenvwrapper.sh\n\n$ source ~\/.bashrc<\/pre>\n<h5>5. \u4f7f\u7528\u865a\u62df\u73af\u5883<\/h5>\n<pre class=\"lang:default decode:true\">mkvirtualenv soc #\u521b\u5efa\u8fd0\u884c\u73af\u5883soc\nworkon soc #\u5de5\u4f5c\u5728 soc \u73af\u5883 \u6216 \u4ece\u5176\u5b83\u73af\u5883\u5207\u6362\u5230 soc \u73af\u5883\ndeactivate #\u9000\u51fa\u7ec8\u7aef\u73af\u5883\nrmvirtualenv soc #\u5220\u9664\u8fd0\u884c\u73af\u5883soc\n\nlsvirtualenv #\u5217\u51fa\u53ef\u7528\u7684\u8fd0\u884c\u73af\u5883\nlssitepackages #\u5217\u51fa\u5f53\u524d\u73af\u5883\u5b89\u88c5\u4e86\u7684\u5305<\/pre>\n<h5>6. \u5f00\u59cbDjango\u9879\u76ee\u7684\u5f00\u53d1<\/h5>\n<pre class=\"lang:default decode:true \">#1. \u65b0\u5efa\u4e00\u4e2a django project\n$ django-admin.py startproject sec_op_center\n$ cd sec_op_center\/\n\n#2. \u65b0\u5efa app\n$ django-admin.py startapp bash_app\n$ vim sec_op_center\/settings.py #\u5c06\u65b0\u5b9a\u4e49\u7684app\u540d\u79f0(bash_app)\u52a0\u5230settings.py\u4e2d\u7684INSTALL_APPS\u4e2d\n$ vim bash_app\/views.py #\u5b9a\u4e49\u89c6\u56fe\u51fd\u6570\uff08\u8bbf\u95ee\u9875\u9762\u65f6\u7684\u5185\u5bb9\uff09\n$ vim sec_op_center\/urls.py #\u5b9a\u4e49\u89c6\u56fe\u51fd\u6570\u76f8\u5173\u7684URL[\u5373\uff1a\u8bbf\u95ee\u4ec0\u4e48\u7f51\u5740\u5bf9\u5e94\u4ec0\u4e48\u5185\u5bb9]\n\n#2.x \u518d\u5efa\u4e00\u4e2a app\n$ django-admin.py startapp idc_app\n$ vim sec_op_center\/settings.py #\u5c06\u65b0\u5b9a\u4e49\u7684app\u540d\u79f0(idc_app)\u52a0\u5230settings.py\u4e2d\u7684INSTALL_APPS\u4e2d\n$ vim idc_app\/views.py #\u5b9a\u4e49\u89c6\u56fe\u51fd\u6570\uff08\u8bbf\u95ee\u9875\u9762\u65f6\u7684\u5185\u5bb9\uff09\n$ vim sec_op_center\/urls.py #\u5b9a\u4e49\u89c6\u56fe\u51fd\u6570\u76f8\u5173\u7684URL[\u5373\uff1a\u8bbf\u95ee\u4ec0\u4e48\u7f51\u5740\u5bf9\u5e94\u4ec0\u4e48\u5185\u5bb9]\n\n#3. \u6570\u636e\u5e93\u914d\u7f6e&amp;\u540c\u6b65\u6570\u636e\u5e93\n$ vim sec_op_center\/settings.py\nDATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.sqlite3',\n        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),\n    },\n    'db1': {\n        'ENGINE': 'django.db.backends.mysql',\n        'NAME': 'dbname1',\n        'USER': 'your_db_user_name',\n        'PASSWORD': 'yourpassword',\n        \"HOST\": \"localhost\",\n    },\n    'db2': {\n        'ENGINE': 'django.db.backends.mysql',\n        'NAME': 'dbname2',\n        'USER': 'your_db_user_name',\n        'PASSWORD': 'yourpassword',\n        \"HOST\": \"localhost\",\n    },\n}\n\n# use multi-database in django\nDATABASE_ROUTERS = ['project_name.database_router.DatabaseAppsRouter']\nDATABASE_APPS_MAPPING = {\n    #'app_name':'database_name',\n    'app1': 'db1',\n    'app2': 'db2',\n}\n# http:\/\/www.ziqiangxuetang.com\/django\/django-multi-database.html\n\n$ python manage.py makemigrations\n$ python manage.py migrate\n\n#4. \u8fd0\u884c\u6d4b\u8bd5\n$ python manage.py runserver 0.0.0.0:8080<\/pre>\n<h5>7.\u4e00\u4e2aDjango\u9879\u76ee(project)\u4e2d\u5404\u5e94\u7528(app)\u5982\u4f55\u57fa\u4e8e\u4e00\u5957\u57fa\u7840\u6a21\u677f\u8fdb\u884c\u5f00\u53d1<\/h5>\n<p>Django \u6a21\u677f\u67e5\u627e\u673a\u5236\uff1a Django \u67e5\u627e\u6a21\u677f\u7684\u8fc7\u7a0b\u662f\u5728\u6bcf\u4e2a app \u7684 templates \u6587\u4ef6\u5939\u4e2d\u627e\uff08\u800c\u4e0d\u53ea\u662f\u5f53\u524d app \u4e2d\u7684\u4ee3\u7801\u53ea\u5728\u5f53\u524d\u7684 app \u7684 templates \u6587\u4ef6\u5939\u4e2d\u627e\uff09\u3002\u5404\u4e2a app \u7684 templates \u5f62\u6210\u4e00\u4e2a\u6587\u4ef6\u5939\u5217\u8868\uff0cDjango \u904d\u5386\u8fd9\u4e2a\u5217\u8868\uff0c\u4e00\u4e2a\u4e2a\u6587\u4ef6\u5939\u8fdb\u884c\u67e5\u627e\uff0c\u5f53\u5728\u67d0\u4e00\u4e2a\u6587\u4ef6\u5939\u627e\u5230\u7684\u65f6\u5019\u5c31\u505c\u6b62\uff0c\u6240\u6709\u7684\u90fd\u904d\u5386\u5b8c\u4e86\u8fd8\u627e\u4e0d\u5230\u6307\u5b9a\u7684\u6a21\u677f\u7684\u65f6\u5019\u5c31\u662f Template Not Found \uff08\u8fc7\u7a0b\u7c7b\u4f3c\u4e8ePython\u627e\u5305\uff09\u3002\u8fd9\u6837\u8bbe\u8ba1\u6709\u5229\u5f53\u7136\u4e5f\u6709\u5f0a\uff0c\u6709\u5229\u662f\u7684\u5730\u65b9\u662f\u4e00\u4e2aapp\u53ef\u4ee5\u7528\u53e6\u4e00\u4e2aapp\u7684\u6a21\u677f\u6587\u4ef6\uff0c\u5f0a\u662f\u6709\u53ef\u80fd\u4f1a\u627e\u9519\u4e86\u3002\u6240\u4ee5\u6211\u4eec\u4f7f\u7528\u7684\u65f6\u5019\u5728 templates \u4e2d\u5efa\u7acb\u4e00\u4e2a app \u540c\u540d\u7684\u6587\u4ef6\u5939\uff0c\u8fd9\u6837\u5c31\u597d\u4e86\u3002\u8fd9\u5c31\u9700\u8981\u628a\u6bcf\u4e2aapp\u4e2d\u7684 templates \u6587\u4ef6\u5939\u4e2d\u518d\u5efa\u4e00\u4e2a app \u7684\u540d\u79f0\uff0c\u4ec5\u548c\u8be5app\u76f8\u5173\u7684\u6a21\u677f\u653e\u5728 app\/templates\/app\/ \u76ee\u5f55\u4e0b\u9762\uff0c\u5728\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u6a21\u677f\u5c31\u662f &#8220;app1\/index.html&#8221; \u548c &#8220;app2\/index.html&#8221; \u8fd9\u6837\u6709app\u4f5c\u4e3a\u540d\u79f0\u7684\u4e00\u90e8\u5206\uff0c\u5c31\u4e0d\u4f1a\u6df7\u6dc6\u3002<\/p>\n<p>Django\u9879\u76ee\u7684app\u7684\u6a21\u677f\u4e00\u822c\u653e\u5728\u81ea\u5df1app\u76ee\u5f55\u4e0b\u7684templates\u4e2d\uff0cDjango\u4f1a\u81ea\u52a8\u53bb\u8fd9\u4e2a\u6587\u4ef6\u5939\u4e2d\u627e\u3002<\/p>\n<p>\u63a8\u8350\u5c06\u591a\u4e2aapp\u5171\u7528\u7684\u57fa\u7840\u6a21\u677f\u6587\u4ef6\u653e\u5728\u7531\u9879\u76ee\u7684 settings.py \u6587\u4ef6\u4e2d\u6307\u5b9a\u7684 STATIC_URL \u6240\u5728\u76ee\u5f55\u4e0b\uff0c\u7136\u540e\u5728\u5404\u4e2aapp\u4e0b\u7684 templates \u6587\u4ef6\u5939\u4e2d\u5b58\u653e\u3001\u7f16\u8f91\u5404\u81ea\u7684\u5185\u5bb9\u6a21\u677f\u6587\u4ef6\u5373\u53ef\u5728\u591a\u5e94\u7528\u7684\u60c5\u51b5\u4e0b\u57fa\u4e8e\u4e00\u5957\u6a21\u677f\u8fdb\u884c\u5f00\u53d1\u3002<\/p>\n<ul>\n<li><a href=\"http:\/\/www.ziqiangxuetang.com\/django\/django-template.html\">http:\/\/www.ziqiangxuetang.com\/django\/django-template.html<\/a><\/li>\n<li><a href=\"http:\/\/www.ziqiangxuetang.com\/django\/django-static-files.html\">http:\/\/www.ziqiangxuetang.com\/django\/django-static-files.html<\/a><\/li>\n<\/ul>\n<h5>8.\u5f85\u6dfb\u52a0<\/h5>\n<p>\u2026\u2026<\/p>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u603b\u7ed3\u4ee5\u5f80\u7ecf\u9a8c\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003 \u6b63\u6587\uff1a 1. \u7f16\u8bd1\u5b89\u88c5 Python 2.7 2. \u5b89\u88c5  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[627,686],"class_list":["post-3005","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-django","tag-templates"],"views":8228,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3005"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3005\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}