参考:https://docs.djangoproject.com/en/4.0/intro/install/
第一步:确认已经安装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
这一步由于网络问题可能会出现连接超时报错,只能重试:
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
第三步:测试效果
>>> import django
>>> print(django.get_version())
4.0
第四步:创建Django项目
% django-admin startproject HelloWorld
% cd HelloWorld
% tree
.
├── HelloWorld
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
HelloWorld: 项目的容器。
manage.py: 一个实用的命令行工具,可让你以各种方式与该 Django 项目进行交互。
HelloWorld/__init__.py: 一个空文件,告诉 Python 该目录是一个 Python 包。
HelloWorld/asgi.py: 一个 ASGI 兼容的 Web 服务器的入口,以便运行你的项目。
HelloWorld/settings.py: 该 Django 项目的设置/配置。
HelloWorld/urls.py: 该 Django 项目的 URL 声明; 一份由 Django 驱动的网站"目录"。
HelloWorld/wsgi.py: 一个 WSGI 兼容的 Web 服务器的入口,以便运行你的项目。
- 启动Web服务
% 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
再运行就不会报错了。
% 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.
以上显示就是正常运行了,我们可以访问测试一下:
% 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
返回状态码200表示成功!!!
或者通过浏览器访问:
上一课 1.1 在MAC下Docker安装Django
下一课 1.3 在ubuntu上安装Django
Django开发教程-目录大纲