方法一:直接安装
pip install django
方法二:使用 requirements.txt 文件安装(推荐)
# 创建并进入工程根目录
touch django-demo
cd django-demo
# 先创建个虚拟环境
virtualenv venv
source venv/bin/activate
# 使用echo命令将pip-tools和django库写入requirements.txt文件,-e 为开启转义字符参数, 以便换行。
echo -e 'pip-tools\ndjango==3.0.4'>requirements.txt
# 生成 requirements.txt 文件, 包含要安装库及其依赖库,并使用豆瓣源镜像加速
pip-compile --index-url=http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com requirements.in
# 安装依赖
pip install -r requirements.txt --index-url=http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com
查看django版本
方法一:在python解释器使用django函数查看
import django
print(django.get_version())
方法二:在Shell中查看
python -m django --version
1.创建新项目
startproject
命令 创建Demo项目
django-admin startproject demo
cd demo
使用 tree
命令查看目录树,看到生成的文件:
.
├── demo
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
使用 runserver
命令运行测试
运行测试 python manage.py runserver
- 更新源码文件后会自动加载服务器,不需要重新启动服务器, 添加新文件除外。
使用浏览器访问 127.0.0.1:8000