一、Django创建app
1)使用命令行式创建
python .\manage.py startapp app01
1.1) windows系统的命令窗口,运行项目的命令
python .\manage.py runserver 127.0.0.1:8001
提示:如果python manage.py runserver 启动失败,说明默认的8000端口被占用(比如酷狗它会占用8000端口)
2) app的目录架构
app是主要写业务架构
image.png
二、Django的路由器
1)django的路由匹配部分不同的版本可能会不一致
2)path和repath的区别
3)解决重复定义的问题:
起别名,避免重复
image.png
4)路由分发(重要)
使用include来进行路由的分发
image.png
5) 反向解析
image.png
image.png
三、Django的视图
视图是处理业务逻辑相关的
视图层中有两个重要的对象:请求对象(HttpRequest)与响应对象(HttpResponse)
##### request.GET 用来获取get请求发送到额数据
# print(request.GET) ### <QueryDict: {'name': ['szk'], 'age': ['12']}>
# print(request.GET.get('name'))
# print(request.GET.get('age'))
#### request.POST 用来获取post请求发送到额数据 Content-Type: application/x-www-form-urlencoded
print(request.POST) ## QueryDict: < {'username': ['szk'], 'passwd': ['123qwe']}>
print(request.POST.get('name'))
如下非常重要
#### request.POST 用来获取post请求发送到额数据 Content-Type: application/x-www-form-urlencoded
print(request.body) ### b'username=szk&passwd=123qwe'
print(request.POST) ## QueryDict: < {'username': ['szk'], 'passwd': ['123qwe']}>
print(request.POST.get('username'))
### 当用户发送post请求的时候,请求体中的数据先到request.body中,
### 然后接下来判断用户的请求体: Content-Type: application/x-www-form-urlencoded,
### 那此时django会将request.body中的数据赋值给request.POST
### 因为有的时候,用户发送的虽然是post请求, 但是用户的请求体是: Content-Type: application/json,那此时request.POST中没有值
### requests.post('http://127.0.0.1:8000/app01/')