{"id":51353,"date":"2024-10-08T17:54:15","date_gmt":"2024-10-08T09:54:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/51353\/"},"modified":"2024-11-14T10:43:38","modified_gmt":"2024-11-14T02:43:38","slug":"ubuntu-16-04-lts-django-%e8%99%9b%e6%93%ac%e9%96%8b%e7%99%bc%e7%92%b0%e5%a2%83%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/51353\/","title":{"rendered":"Ubuntu 16.04 LTS Django \u865b\u64ec\u958b\u767c\u74b0\u5883\u914d\u7f6e"},"content":{"rendered":"<h1 id=\"ubuntu-16-04-lts-django-%e8%99%9b%e6%93%ac%e9%96%8b%e7%99%bc%e7%92%b0%e5%a2%83%e9%85%8d%e7%bd%ae-wFOEzQvKWy\">Ubuntu 16.04 LTS Django \u865b\u64ec\u958b\u767c\u74b0\u5883\u914d\u7f6e<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u9801\u958b\u767c\u4e2d\uff0cDjango \u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684 Python \u7db2\u9801\u6846\u67b6\uff0c\u53d7\u5230\u8d8a\u4f86\u8d8a\u591a\u958b\u767c\u8005\u7684\u9752\u775e\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Ubuntu 16.04 LTS \u4e0a\u914d\u7f6e Django \u7684\u865b\u64ec\u958b\u767c\u74b0\u5883\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2 id=\"%e5%89%8d%e6%9c%9f%e6%ba%96%e5%82%99-wFOEzQvKWy\">\u524d\u671f\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u78ba\u4fdd\u4f60\u7684 Ubuntu 16.04 \u7cfb\u7d71\u5df2\u7d93\u66f4\u65b0\u5230\u6700\u65b0\u7248\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u66f4\u65b0\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt upgrade<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e8%a3%9d-python-%e5%92%8c-pip-wFOEzQvKWy\">\u5b89\u88dd Python \u548c pip<\/h2>\n<p>Django \u662f\u57fa\u65bc Python \u7684\uff0c\u56e0\u6b64\u9700\u8981\u5148\u5b89\u88dd Python\u3002Ubuntu 16.04 \u9810\u8a2d\u5df2\u7d93\u5b89\u88dd\u4e86 Python 2.7 \u548c Python 3.x\uff0c\u4f46\u6211\u5011\u5efa\u8b70\u4f7f\u7528 Python 3\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd Python 3 \u548c pip\uff1a<\/p>\n<pre><code>sudo apt install python3 python3-pip<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e8%a3%9d%e8%99%9b%e6%93%ac%e7%92%b0%e5%a2%83-wFOEzQvKWy\">\u5b89\u88dd\u865b\u64ec\u74b0\u5883<\/h2>\n<p>\u70ba\u4e86\u907f\u514d\u4f9d\u8cf4\u885d\u7a81\uff0c\u5efa\u8b70\u4f7f\u7528\u865b\u64ec\u74b0\u5883\u4f86\u7ba1\u7406 Django \u5c08\u6848\u7684\u4f9d\u8cf4\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd\u865b\u64ec\u74b0\u5883\u5de5\u5177\uff1a<\/p>\n<pre><code>sudo apt install python3-venv<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5275\u5efa\u4e00\u500b\u65b0\u7684\u865b\u64ec\u74b0\u5883\u3002\u5047\u8a2d\u6211\u5011\u7684\u5c08\u6848\u540d\u7a31\u70ba <code>myproject<\/code>\uff1a<\/p>\n<pre><code>mkdir ~\/myproject\ncd ~\/myproject\npython3 -m venv venv<\/code><\/pre>\n<p>\u555f\u52d5\u865b\u64ec\u74b0\u5883\uff1a<\/p>\n<pre><code>source venv\/bin\/activate<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e8%a3%9d-django-wFOEzQvKWy\">\u5b89\u88dd Django<\/h2>\n<p>\u5728\u865b\u64ec\u74b0\u5883\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 pip \u5b89\u88dd Django\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd\u6700\u65b0\u7248\u672c\u7684 Django\uff1a<\/p>\n<pre><code>pip install django<\/code><\/pre>\n<h2 id=\"%e5%89%b5%e5%bb%ba-django-%e5%b0%88%e6%a1%88-wFOEzQvKWy\">\u5275\u5efa Django \u5c08\u6848<\/h2>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528 Django \u63d0\u4f9b\u7684\u547d\u4ee4\u884c\u5de5\u5177\u5275\u5efa\u4e00\u500b\u65b0\u7684\u5c08\u6848\uff1a<\/p>\n<pre><code>django-admin startproject mysite<\/code><\/pre>\n<p>\u9032\u5165\u5c08\u6848\u76ee\u9304\uff1a<\/p>\n<pre><code>cd mysite<\/code><\/pre>\n<h2 id=\"%e9%81%8b%e8%a1%8c%e9%96%8b%e7%99%bc%e4%bc%ba%e6%9c%8d%e5%99%a8-wFOEzQvKWy\">\u904b\u884c\u958b\u767c\u4f3a\u670d\u5668<\/h2>\n<p>\u5728\u5c08\u6848\u76ee\u9304\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u555f\u52d5 Django \u7684\u958b\u767c\u4f3a\u670d\u5668\uff1a<\/p>\n<pre><code>python manage.py runserver<\/code><\/pre>\n<p>\u9ed8\u8a8d\u60c5\u6cc1\u4e0b\uff0c\u958b\u767c\u4f3a\u670d\u5668\u6703\u5728 <code>http:\/\/127.0.0.1:8000\/<\/code> \u4e0a\u904b\u884c\u3002\u6253\u958b\u700f\u89bd\u5668\uff0c\u8f38\u5165\u8a72\u5730\u5740\uff0c\u4f60\u61c9\u8a72\u80fd\u770b\u5230 Django \u7684\u6b61\u8fce\u9801\u9762\u3002<\/p>\n<h2 id=\"%e9%85%8d%e7%bd%ae%e6%95%b8%e6%93%9a%e5%ba%ab-wFOEzQvKWy\">\u914d\u7f6e\u6578\u64da\u5eab<\/h2>\n<p>Django \u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u5305\u62ec SQLite\u3001PostgreSQL \u548c MySQL\u3002\u9ed8\u8a8d\u60c5\u6cc1\u4e0b\uff0cDjango \u4f7f\u7528 SQLite \u4f5c\u70ba\u6578\u64da\u5eab\u3002\u5982\u679c\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u6578\u64da\u5eab\uff0c\u53ef\u4ee5\u5728 <code>settings.py<\/code> \u6587\u4ef6\u4e2d\u9032\u884c\u914d\u7f6e\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u82e5\u8981\u4f7f\u7528 PostgreSQL\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd PostgreSQL \u548c\u76f8\u61c9\u7684 Python \u9a45\u52d5\uff1a<\/p>\n<pre><code>sudo apt install postgresql postgresql-contrib\npip install psycopg2<\/code><\/pre>\n<p>\u7136\u5f8c\u5728 <code>settings.py<\/code> \u4e2d\u4fee\u6539\u6578\u64da\u5eab\u914d\u7f6e\uff1a<\/p>\n<pre><code>DATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.postgresql',\n        'NAME': 'your_db_name',\n        'USER': 'your_db_user',\n        'PASSWORD': 'your_db_password',\n        'HOST': 'localhost',\n        'PORT': '',\n    }\n}<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-wFOEzQvKWy\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u6210\u529f\u5728 Ubuntu 16.04 LTS \u4e0a\u914d\u7f6e\u4e86 Django \u7684\u865b\u64ec\u958b\u767c\u74b0\u5883\u3002\u9019\u6a23\u7684\u74b0\u5883\u4e0d\u50c5\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u958b\u5c55\u5c08\u6848\uff0c\u9084\u80fd\u6709\u6548\u7ba1\u7406\u4f9d\u8cf4\uff0c\u907f\u514d\u885d\u7a81\u3002\u5982\u679c\u4f60\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u4f86\u90e8\u7f72\u4f60\u7684 Django \u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u958b\u767c\u8005\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to set up a virtual development environment for Django on Ubuntu 16.04 LTS, ensuring efficient and organized project management.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-51353","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51353","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=51353"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51353\/revisions"}],"predecessor-version":[{"id":51354,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51353\/revisions\/51354"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=51353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=51353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=51353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}