开发环境

  • 系统版本: Windows11
  • CPU: i5 1135G7
  • Python版本: 3.7.7

开始部署

安装模块

安装之前可以先看: pip配置镜像加速地址

打开CMD执行下面的命令

python -m pip install Django

安装效果

C:\Users\Y>python -m pip install Django
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting Django
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9c/0e/02b7eff8fac2c25ede489933d4e899f6e6f283ae8eaf5189431057c8d406/Django-3.2.12-py3-none-any.whl (7.9 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.9/7.9 MB 3.2 MB/s eta 0:00:00
Collecting pytz
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d3/e3/d9f046b5d1c94a3aeab15f1f867aa414f8ee9d196fae6865f1d6a0ee1a0b/pytz-2021.3-py2.py3-none-any.whl (503 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 503.5/503.5 KB 2.3 MB/s eta 0:00:00
Collecting sqlparse>=0.2.2
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/05/40/d836d55fb3f467243ee839ab7b814822fda522cd395fa41e282684e71ee5/sqlparse-0.4.2-py3-none-any.whl (42 kB)
Collecting asgiref<4,>=3.3.2
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/0b/9f/5f3b91391578312827561b669a0397d58535b4e82966c8f1667525c7d563/asgiref-3.5.0-py3-none-any.whl (22 kB)
Collecting typing-extensions
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/45/6b/44f7f8f1e110027cf88956b59f2fad776cca7e1704396d043f89effd3a0e/typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Installing collected packages: pytz, typing-extensions, sqlparse, asgiref, Django
  WARNING: The script sqlformat.exe is installed in 'C:\Users\Y\AppData\Roaming\Python\Python37\Scripts' 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.exe is installed in 'C:\Users\Y\AppData\Roaming\Python\Python37\Scripts' 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-3.2.12 asgiref-3.5.0 pytz-2021.3 sqlparse-0.4.2 typing-extensions-4.1.1

配置环境变量

在安装的时候出现警告信息:

WARNING: The script sqlformat.exe is installed in 'C:\Users\Y\AppData\Roaming\Python\Python37\Scripts' which is not on PATH.

此时只需要把路径加入系统变量:PATH即可

C:\Users\Y\AppData\Roaming\Python\Python37\Scripts

如下
image.png

查看框架版本

C:\Users\Y>python -m django --version
3.2.12

创建项目

语法如下

django-admin startproject <mysite>
  • <mysite> : 项目名称,请根据实际情况定义

例如

D:\DATA\BaiduNetdiskWorkspace\Code\xc\printerdata\web>django-admin startproject websearch
D:\DATA\BaiduNetdiskWorkspace\Code\xc\printerdata\web>

创建成功是不会有提示的,程序会自动在当前目录创建一个文件夹,以项目名称命名

 D:\DATA\BaiduNetdiskWorkspace\Code\xc\printerdata\web 的目录

2022/03/19  13:10    <DIR>          .
2022/03/19  12:41    <DIR>          ..
2022/03/19  12:56               162 index.py
2022/03/19  13:10    <DIR>          websearch
2022/03/18  18:49                18 目录说明.txt
               2 个文件            180 字节
               3 个目录 408,402,968,576 可用字节

运行项目

创建之后,执行下面的命令即可运行服务

需要进入项目目录执行

D:\DATA\BaiduNetdiskWorkspace\Code\xc\printerdata\web>cd websearch

D:\DATA\BaiduNetdiskWorkspace\Code\xc\printerdata\web\websearch> py manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

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.
March 19, 2022 - 13:13:08
Django version 3.2.12, using settings 'websearch.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

运行效果

根据信息打开网址:

Starting development server at http://127.0.0.1:8000/

image.png

更换端口

默认情况下,runserver 命令会将服务器设置为监听本机内部 IP 的 8000 端口。

如果你想更换服务器的监听端口,请使用命令行参数。举个例子,下面的命令会使服务器监听 8080 端口:

py manage.py runserver 8080

如果你想要修改服务器监听的IP,在端口之前输入新的。比如,为了监听所有服务器的公开IP(这你运行 Vagrant 或想要向网络上的其它电脑展示你的成果时很有用),使用:

py manage.py runserver 0:8000

0 是 0.0.0.0 的简写。完整的关于开发服务器的文档可以在 :
djamdin:runserver
参考文档中找到。

本文部分内容参考:

编写你的第一个 Django 应用,第 1 部分¶