參考:https://docs.djangoproject.com/en/4.0/intro/install/
第一步:確認(rèn)已經(jīng)安裝python3和pip
% python3
Python 3.9.12 (main, Mar 26 2022, 15:51:13)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
% pip --version
pip 22.0.4 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
第二步:安裝django
% sudo python3 -m pip install Django
Defaulting to user installation because normal site-packages is not writeable
Collecting Django
Downloading Django-4.0.5-py3-none-any.whl (8.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.0/8.0 MB 29.9 kB/s eta 0:00:00
Collecting asgiref<4,>=3.4.1
Downloading asgiref-3.5.2-py3-none-any.whl (22 kB)
Collecting sqlparse>=0.2.2
Downloading sqlparse-0.4.2-py3-none-any.whl (42 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.3/42.3 KB 36.6 kB/s eta 0:00:00
Installing collected packages: sqlparse, asgiref, Django
WARNING: The script sqlformat is installed in '/home/duye/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script django-admin is installed in '/home/duye/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed Django-4.0.5 asgiref-3.5.2 sqlparse-0.4.2
這一步由于網(wǎng)絡(luò)問題可能會出現(xiàn)連接超時報(bào)錯重慢,只能重試:
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
第三步:測試效果
>>> import django
>>> print(django.get_version())
4.0
第四步:創(chuàng)建Django項(xiàng)目
% django-admin startproject HelloWorld
% cd HelloWorld
% tree
.
├── HelloWorld
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
HelloWorld: 項(xiàng)目的容器。
manage.py: 一個實(shí)用的命令行工具逊躁,可讓你以各種方式與該 Django 項(xiàng)目進(jìn)行交互似踱。
HelloWorld/__init__.py: 一個空文件,告訴 Python 該目錄是一個 Python 包稽煤。
HelloWorld/asgi.py: 一個 ASGI 兼容的 Web 服務(wù)器的入口核芽,以便運(yùn)行你的項(xiàng)目。
HelloWorld/settings.py: 該 Django 項(xiàng)目的設(shè)置/配置酵熙。
HelloWorld/urls.py: 該 Django 項(xiàng)目的 URL 聲明; 一份由 Django 驅(qū)動的網(wǎng)站"目錄"轧简。
HelloWorld/wsgi.py: 一個 WSGI 兼容的 Web 服務(wù)器的入口,以便運(yùn)行你的項(xiàng)目匾二。
- 啟動Web服務(wù)
% python3 manage.py runserver 0.0.0.0:8000
啟動時會提示如下錯如:
You have 18 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.
解決辦法:
% python3 manage.py migrate
再運(yùn)行就不會報(bào)錯了哮独。
% python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
June 05, 2022 - 14:58:20
Django version 4.0.5, using settings 'HelloWorld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
以上顯示就是正常運(yùn)行了,我們可以訪問測試一下:
% curl http://127.0.0.1:8000/ -vv >/dev/null
* Trying 127.0.0.1:8000...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Sun, 05 Jun 2022 15:49:05 GMT
< Server: WSGIServer/0.2 CPython/3.10.4
< Content-Type: text/html
< X-Frame-Options: DENY
< Content-Length: 10697
< X-Content-Type-Options: nosniff
< Referrer-Policy: same-origin
< Cross-Origin-Opener-Policy: same-origin
<
{ [10697 bytes data]
100 10697 100 10697 0 0 1615k 0 --:--:-- --:--:-- --:--:-- 1741k
* Connection #0 to host 127.0.0.1 left intact
返回狀態(tài)碼200表示成功2烀辍Fよ怠!
或者通過瀏覽器訪問:
上一課 1.1 在MAC下Docker安裝Django
下一課 1.3 在ubuntu上安裝Django
Django開發(fā)教程-目錄大綱