MVT
m: model,负责与数据库交互
v:view ,核心,负责接收请求/获取数据/返回结果
t:template,负责呈现内容到浏览器
安装Django
[root@mdns ~]#pip3 install django==2.2.12
[root@mdns ~]#pip3 freeze|grep -i 'Django'
Django==2.2.12
创建项目
成功安装后,会有django-admin命令
django-admin startproject 项目名,即可创建出对应项目文件夹
[root@mdns zaishu]#django-admin startproject mysite1
[root@mdns zaishu]#ls
mysite1
启动服务
[root@mdns zaishu]#cd mysite1/
[root@mdns mysite1]#python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 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.
February 11, 2022 - 23:11:08
Django version 2.2.12, using settings 'mysite1.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
开启新端口
[root@mdns mysite1]#python3 manage.py runserver 5000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 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.
February 11, 2022 - 23:12:30
Django version 2.2.12, using settings 'mysite1.settings'
Starting development server at http://127.0.0.1:5000/
Quit the server with CONTROL-C.
[root@mdns ~]#lsof -i:8000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3 1947 root 4u IPv4 29954 0t0 TCP localhost:irdmi (LISTEN)
关闭服务
项目结构
[root@mdns zaishu]#tree mysite1/
mysite1/
├── db.sqlite3
├── manage.py
└── mysite1
├── __init__.py
├── __pycache__
│?? ├── __init__.cpython-36.pyc
│?? ├── settings.cpython-36.pyc
│?? ├── urls.cpython-36.pyc
│?? └── wsgi.cpython-36.pyc
├── settings.py
├── urls.py
└── wsgi.py
2 directories, 10 files
- mange.py
python3 manage.py runserver : 启动服务
python3 manage.py startapp: 创建应用
python3 manage.py migrate: 数据库迁移
.....
python3 manage.py: 列出django子命令
- 项目同名文件夹-mysite1/mysite1
__init__:python3包的初始化文件
wsgi.py:WEB服务网关的配置文件-django正式启动时,需要用到
urls.py:项目的主路由配置-Http请求进入django时,优先调用该文件
settings.py:项目的配置文件-包含项目启动时需要的配置
- settings.py - 包含django项目启动的所有配置项
配置项分为 公有配置 和 自定义配置
配置项格式例子:BASE_DIR= 'xxx'
公有配置-Django官方提供的基础配置
常见配置-DEBUG
DEBUG模式,会提供报错页面
DEBUG = False
# True- 调试模式,调试模式能检测代码修改,自动重启生效;提供报错页面
# False - 正式启动,上线模式。
ALLOWED_HOSTS = [127.0.0.1]
则不会提供报错页面
跟地址栏的地址需要保持一致
常见配置-BASE_DIR
常见配置-ALLOWED_HOSTS
允许外网访问
如果要让其他主机也能访问主机的Django服务:
[root@mdns mysite1]#grep -i 'ALLOWED_HOSTS' mysite1/settings.py
ALLOWED_HOSTS = ['192.168.47.11','127.0.0.1']
# 用0.0.0.0的方式启动端口
[root@mdns mysite1]#python3 manage.py runserver 0.0.0.0:5000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 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.
February 13, 2022 - 10:07:29
Django version 2.2.12, using settings 'mysite1.settings'
Starting development server at http://0.0.0.0:5000/
Quit the server with CONTROL-C.
其他配置
INSTALLED_APPS : 指定当前项目中安装的应用列表
MIDDLEWARE: 用于注册中间件
TEMPLATES: 用于指定模板的配置信息
DATABASES: 用于指定数据库的配置信息
LANGUAGE_CODE:用于指定语言配置
中文: “en-us”
中文: “zh-Hans”
TIME_ZONE:用于指定当前服务器端时区
- 世界标准时间: “en-us”
- 中国时区: “Asia/Shanghai”
[root@mdns mysite1]#grep -i 'TIME_ZONE' mysite1/settings.py
TIME_ZONE = "Asia/Shanghai"
ROOT_URLCONF:用于配置主url,默认是mysite1.urls
[root@mdns mysite1]#grep -i 'ROOT_URLCONF' mysite1/settings.py
ROOT_URLCONF = 'mysite1.urls'
自定义配置
settings.py也可以额添加开发人员自定义配置
配置建议: 名字尽量个性化 - 防止覆盖掉共有配置
ALIPAY_KEY = ‘xxx’
settings.py中的所有配置项,都可以按需在代码中引用。引入的方式:from django.conf import settings