1. 程式人生 > >建立工程 和 APP 與 開發工具

建立工程 和 APP 與 開發工具

1.安裝

在這裡使用的是Anaconda,直接使用Anaconda命令進行安裝;

(E:\Anaconda3) C:\Users\yuan>conda install django

// 安裝指定版本 django
(E:\Anaconda3) C:\Users\yuan>conda install django=1.9

// pip 安裝指定版本 django
labelnet:~ yuan$ pip3 install django==1.9

當然你也可以看 官方文件

安裝完成可以檢視django版本 :

(E:\Anaconda3) C:\Users\yuan>python -m django --version
1.10.3

2.建立工程

(1)使用下面命令建立Django工程

(E:\Anaconda3) D:\test>django-admin startproject mysite

(2)檢視 mysite工程目錄結構

(E:\Anaconda3) D:\test>tree /f
Folder PATH listing for volume data-one
Volume serial number is 000000AC 263B:D486
D:.
└─mysite   #專案名稱,算是個容器
    │  manage.py  #django的專案管理工具,通過命令列進行操作相關內容
│ └─mysite #與專案同名的總配置資料夾 settings.py #專案配置:app配置,資料配置等 urls.py #總的urls配置,可以配置app中的urls wsgi.py # ? __init__.py #一個空檔案,告訴python是一個python包

(3)執行

使用下面命令執行: 預設埠為 8000

(E:\Anaconda3) D:\test\mysite>python manage.py runserver
Performing system
checks... System check identified no issues (0 silenced). You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. November 24, 2016 - 11:10:07 Django version 1.10.3, using settings 'mysite.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.

當然,也可以指定埠,比如:

(E:\Anaconda3) D:\test\mysite>python manage.py runserver 8080
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
November 24, 2016 - 11:19:07
Django version 1.10.3, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CTRL-BREAK.

or 指定埠和IP

python manage.py runserver 0.0.0.0:8000

(4)效果

這裡寫圖片描述

2.建立App

(1)使用下面命令建立App

建立名為 testapp 的 app

(E:\Anaconda3) D:\test\mysite>python manage.py startapp testapp

(2)檢視目錄結構

(E:\Anaconda3) D:\test\mysite>tree /f
Folder PATH listing for volume data-one
Volume serial number is 0000006E 263B:D486
D:.
│  db.sqlite3 #建立app後,預設使用的是sqlite資料庫,會生成該檔案
│  manage.py
│
├─mysite
│  │  settings.py
│  │  urls.py
│  │  wsgi.py
│  │  __init__.py
│  │
│  └─__pycache__   #執行python指令碼後,生成的可執行檔案儲存位置
│          settings.cpython-35.pyc
│          urls.cpython-35.pyc
│          wsgi.cpython-35.pyc
│          __init__.cpython-35.pyc
│
└─testapp    #app名稱
    │  admin.py #管理員相關 : ?
    │  apps.py #app相關: ?
    │  models.py #模型,可定義資料庫表,通過配置後,執行命令,生成資料庫表; M :模型
    │  tests.py #單元測試相關
    │  views.py #主要以資料庫操作相關 ;C : 控制器 
    │  __init__.py
    │
    └─migrations #models.py 中的表發生改變,生成資料庫的時候,這裡會有記錄
            __init__.py

(3)關於 pycache目錄

當第一次執行 python 指令碼時,直譯器會將 *.py 指令碼進行編譯並儲存到 __pycache__ 目錄 下次執行指令碼時,若直譯器發現你的 *.py 指令碼沒有變更,便會跳過編譯一步,直接執行儲存在 __pycache__ 目錄下的 *.pyc 檔案

關閉 pycache

單次關閉 : 執行指令碼時新增 -B 引數即可
永久關閉 : 設定環境變數 PYTHONDONTWRITEBYTECODE=1 即可

3.開發環境

使用PyCharm進行開發,下載安裝專業版,不要使用社群版(django各種不支援,可以寫python);

專業版啟用碼 (來源 INTERNET):

43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiIiwiYXNzaWduZWVFbWFpbCI6IiIsImxpY2Vuc2VSZXN0cmljdGlvbiI6IkZvciBlZHVjYXRpb25hbCB1c2Ugb25seSIsImNoZWNrQ29uY3VycmVudFVzZSI6ZmFsc2UsInByb2R1Y3RzIjpbey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cGNExHe82kjF4zwfadHk3bQVvbfdAwxcDy4xBjs3L4raPLU3yenSzr/OEur1+jfOxnQSmEcMXKXgrAQ9U55gwjcOFKrgOxEdek/Sk1VfOjvS+nuM4eyEruFMfaZHzoQiuw4IqgGc45ohFH0UUyjYcuFxxDSU9lMCv8qdHKm+wnPRb0l9l5vXsCBDuhAGYD6ss+Ga+aDY6f/qXZuUCEUOH3QUNbbCUlviSz6+GiRnt1kA9N2Qachl+2yBfaqUqr8h7Z2gsx5LcIf5kYNsqJ0GavXTVyWh7PYiKX4bs354ZQLUwwa/cG++2+wNWP+HtBhVxMRNTdVhSm38AknZlD+PTAsWGu9GyLmhti2EnVwGybSD2Dxmhxk3IPCkhKAK+pl0eWYGZWG3tJ9mZ7SowcXLWDFAk0lRJnKGFMTggrWjV8GYpw5bq23VmIqqDLgkNzuoog==

File -> Open :專案資料夾 mysite , 非 app資料夾;

Tips

當然也可以使用PyCharm專業版 File -> New Project 進行建立專案

這裡寫圖片描述