1.安裝django
1.1、下載Django包
https://www.djangoproject.com/download/
https://www.djangoproject.com/m/releases/1.10/Django-1.10.5.tar.gz
1.2、解壓下載好的壓縮包:Django-1.10.5.tar.gz
直接解壓縮,我解壓縮的目錄是E:盤 E:\Django-1.10.5
PS:在安裝Django之前,請確保你機器上面安裝了python
我機器上面安裝了python3.6.0
1.3、打開windows的終端:運行-->cmd
然后進入到你解壓縮的目錄,輸入命令:
python setup.py install
直接回車,就開始安裝了
1.4、檢測是否安裝成功
python
import django
django.VERSION
print(django.__path__)
如果你要卸載安裝的django,可以直接刪除上面圖中所示的地址(print(django.__path__)所打印的地址)目錄即可
1.5、添加環境變量
配置環境變量還是需要手動,將這幾個目錄添加到系統環境變量中:D:\python;D:D:\python\Lib\site-packages\django;D:\python\Scripts;
其中你install Django后,會在你python的安裝目錄的Lib\site-packages\添加django文件夾,在你python的安裝目錄的添加中添加\Scripts文件夾,你要做的就是把這兩個文件夾添加到環境變量path中,添加好環境變量后,就可以使用Django的django-admin.py命令新建工程了
我本地安裝的是3.6.0版本,安裝完django后D:\python\Lib\site-packages下的django目錄是這樣的:
\Django-1.10.5-py3.6.egg\
\Django-1.10.5-py3.6.egg\django\
所以配置的環境變量如下:
D:\python;D:\python\Scripts;D:\python\Lib\site-packages\Django-1.10.5-py3.6.egg\django;D:\python\Lib\site-packages\Django-1.10.5-py3.6.egg;D:\python\Lib\site-packages\Django-1.10.5-py3.6.egg\django\bin
Ps:執行django-admin.py startproject hello時出現報錯:
python: can't open file 'django-admin.py': [Errno 2] No such file or directory
原因未成功配置環境變量,所以添加了好幾個django目錄,沒搞清最少配置項是什么。
2.創建django helloworld項目
進入cmd命令行,執行代碼:
切換到你想要建立Django應用的路徑下
cd /d D:\hello
執行下面的命令在當前路徑下建立名為hello的應用,就會在當前路徑下建立名為hello的應用了。
django-admin.py startproject hello
命令行切換到剛建立的hello目錄下,打開web服務:
cd hello
manage.py runserver 0.0.0.0:8090
出現如下提示,證明你的應用開啟web服務了
Performing system checks...
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work
apply the migrations for app(s): admin, auth, contenttypes, s
Run 'python manage.py migrate' to apply them.
February 13, 2017 - 09:35:24
Django version 1.10.5, using settings 'hello.settings'
Starting development server at http://0.0.0.0:8090/
Quit the server with CTRL-BREAK.
[13/Feb/2017 09:35:58] "GET / HTTP/1.1" 200 1767
Not Found: /favicon.ico
[13/Feb/2017 09:35:58] "GET /favicon.ico HTTP/1.1" 404 1935
你可以登陸http://127.0.0.1:8090/,看到歡迎頁面就證明成功了
It worked!
Congratulations on your first Django-powered page.
Of course, you haven't actually done any work yet. Next, start your first app by running python manage.py startapp [app_label].
You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!
接下來你就可以開始你的Django開發了