s11day97前端
1. 为何要作先后端分离?vue
- 先后端交给不一样的人来编写,职责划分明确。react
- API (IOS,安卓,PC,微信小程序...)ios
- vue.js等框架编写前端时,会比以前写jQuery更简单快捷。web
2. 对于后端人员,主要为前端提供:API(接口)redis
之前的你的接口:vuex
http://127.0.0.1:8000/index/django
http://127.0.0.1:8000/users/编程
http://127.0.0.1:8000/add_users/json
http://127.0.0.1:8000/del_users/
http://127.0.0.1:8000/edit_users/
restful 规范:
http://127.0.0.1:8000/users/
3. 谈谈你对restful规范的理解?
1. 使用https代替http
https://www.luffycity.com/course/detail/web/3
http://www.luffycity.com/course/detail/web/3
2. 在URL中体现本身写的是API
https://www.luffycity.com/api/
https://api.luffycity.com/ 可能会跨域
3. 在URL中体现版本
https://www.luffycity.com/api/v1/users
https://www.luffycity.com/api/v2/users
4. 名词(面向资源编程)
https://www.luffycity.com/api/v1/users
https://www.luffycity.com/api/v1/song
5. 行为
https://www.luffycity.com/api/v1/users
method:
get,获取
post,新建
put,更新
patch,局部更新
delete,删除
6. 条件
https://www.luffycity.com/api/v1/users?page=1
https://www.luffycity.com/api/v1/users?page=1&gender=2
7. 状态码
200
301
302
404
500
推荐使用code:
def xx(request):
ret = {'code':1000,'data':None}
try:
...
except Exptions as e:
ret['status'] = 1001
ret['error'] = 'xxxx错误'
return JsonResponse(ret)
8. 错误信息
{
code:10001,
error:'用户名或密码错误'
}
9. 返回结果:
GET:
https://www.luffycity.com/api/v1/users
响应:
{
code: 1000,
data: [
{'name':'赵森','age':19},
{'name':'赵云','age':16},
{'name':'赵云','age':16},
{'name':'赵云','age':16},
{'name':'赵云','age':16},
]
}
GET:
https://www.luffycity.com/api/v1/users/1/
响应:
{
code:1000,
data:{'name':'赵森','age':19},
}
POST:
https://www.luffycity.com/api/v1/users
请求体:
{'name':'大表哥','age':19}
响应(不要):
{
code:1000,
data:{'id':9, 'name':'大表哥','age':19}
}
PUT/PATCH:
https://www.luffycity.com/api/v1/users
请求体:
{'name':'大表哥','age':19}
响应(不要):
{
code:1000,
data:{'id':9, 'name':'大表哥','age':19}
}
DELETE:
...
10. hyper link
访问:https://www.luffycity.com/api/v1/users
{
code:1000,
data:[
{'id':1,'name':'赵森','age':19, 'depart':https://www.luffycity.com/api/v1/depart/1/},
{'id':1,'name':'赵森','age':19, 'depart':https://www.luffycity.com/api/v1/depart/1/},
{'id':1,'name':'赵森','age':19, 'depart':https://www.luffycity.com/api/v1/depart/1/},
{'id':1,'name':'赵森','age':19, 'depart':https://www.luffycity.com/api/v1/depart/1/},
{'id':1,'name':'赵森','age':19, 'depart':https://www.luffycity.com/api/v1/depart/1/},
]
}
https://www.luffycity.com/api/v1/users
{
code:1000,
data:[
{'id':1,'name':'赵森','age':19, 'depart_title':'公关部'},
{'id':1,'name':'赵森','age':19, 'depart_title':'公关部'},
{'id':1,'name':'赵森','age':19, 'depart_title':'公关部'},
{'id':1,'name':'赵森','age':19, 'depart_title':'公关部'},
{'id':1,'name':'赵森','age':19, 'depart_title':'公关部'},
]
}
4. django rest framework框架的做用?
帮助开发者能够快速开发出遵循restful规范的API
5. django rest framework框架都有哪些组件(10)?
版本【1】
权限
认证
节流
分页【2】
解析器【3】 ****
序列化 *****
视图 ****
路由
渲染器【4】
项目架构:
- 主站,学生使用;(vue.js + rest framework)
- 导师后台,导师使用;
- 管理后台,运营使用;
开发人员:
主站:
- 前端:
- 前端姑娘 v1,vue.js 1.0
- 前端姑娘 v2,vue.js 2.0
- 后端:
- 老村长
- 产品经理
- alex/我/文周
导师: 1人
管理后台:1人 + 兼职导师
2. ContentType
- http请求头
现象:reqeust.POST中未获取到数据。
a. 老板数据无法来
b. 本身
request.POST
requset.boy
Content-Type请求头的做用?
用于标记请求体数据的格式,如:
1. Content-Type:application/x-www-form-urlencoded
请求体:b'pwd=123&user=root'
2. Content-Type:application/json
请求体:{"pwd": 123, "user": "root"}
s11day98
内容回顾:
1. 为何要作先后端分离?
2. 简述http协议?
- 基于socket
- 数据格式:
"GET /index?name=123&age=19 http1.1\r\nhost:www.luffyciti.com\r\ncontent-type:application/json...\r\n\r\n"
"POST /index http1.1\r\nhost:www.luffyciti.com\r\ncontent-type:application/json...\r\n\r\n{name:'alex',age:18}"
"POST /index http1.1\r\nhost:www.luffyciti.com\r\ncontent-type:application/enform.....\r\n\r\nname=alex&age=18&xx=19"
- 无状态短连接
一次请求一次响应以后断开链接
3. 简述restful 规范?
https://www.luffycity.com/api/v1/courses/?sub_category=0
4. django rest framework组件的做用?
5. 列举django rest framework组件(10)?
6. 路飞的表结构
s11day99
内容回顾:
1. 为何作先后端分离?
2. rest framework做用?
3. 简述Http协议?
4. 列举rest framework组件?
5. restful 规范?
6. content-type请起头的做用?
用于告知服务端,客户端发送的请求体数据格式。
Content-Typeapplication/x-www-form-urlencoded:
请求体格式:phone=8615131255555&password=asdfasdfasdf&oneMonth=1
Content-Type: application/json;charset=UTF-8
请求体格式:{"BaseRequest":{"Uin":981579400,"Sid":"h9kV51dfCuwJy9SX","Skey":"@crypt_2ccf8ab9_edc3756c6a6adef29051ab1ae52c6cb6","DeviceID":"e037891563571357"},"Msg":{"Type":1,"Content":"test","FromUserName":"@3fd34d9c325790b34948028adc36a31f","ToUserName":"@6ba3ce1e58cfb403c9adaf8053e82e79","LocalID":"15336944955110060","ClientMsgId":"15336944955110060"},"Scene":0}
扩展:user-agent请求头?
7. django content-type组件的做用?
解决一张表和多张表作FK关联的问题。
8. 哪里使用过面向对象的封装?
class BaseRequest(object):
def __init__(self):
self.code = ..
self.data =
self.error = ...
...
def dict()
obj = BaseRequest()
obj.__dict__
9. 视图要写
- try
- 注释
- 建明之一
10. ORM
a. FK正向和反向操做
class A:
name = ..;.
clas B:
a = FK(A,related_name='xxxx')
title = ...
class C:
a = FK(A)
age = ...
b. O2O的正向和反向操做
class A:
name = ..;.
class B:
a = O2O(A)
age = ...
c. 补充:models.User.objects.filter(xx__isnull=True)
class 部门用户表:
title = ..;.
clas 用户表:
p = FK(A,related_name='xxxx')
name = ...
modes.用户表.objects.filter('name','p__title')
modes.部门用户表.objects.filter('title','用户表__name',表__isnull=False)
注意:
1. left join和inner join的区别?
2. left join是表在前和在后请求不同?
s11day100
内容回顾:
1. django请求生命周期?
#- wsgi #- 中间件 #- 路由 #- 视图 #- ORM #- 模板渲染
2. django提供的功能
#- 必备 #- 路由 #- 视图 #- 模板渲染
- django:
#- ORM: #... #... #- 分页 #- Form & ModelForm #- admin #- auth #- session #- 中间件 #- contenttype #- csrf #- 缓存(速度块)
3. restful
#- restful 规范 #- django rest framwork #- 其余 #- 跨域
a. 为何出现跨域?
b. 如何解决跨域?
#使用cors,即:设置响应头。 #简单请求: #响应头中设置一个容许域名访问 #复杂请求: #OPTIONS请求作预检,容许特殊请求方式和请求头 + 容许域名访问。 #真正请求就能够发送过来进行处理 + 容许域名访问。
c. 跨域
#www.baidu.com / www.luffycity.com #www.baidu.com / api.luffycity.com #www.baidu.com:8001 / www.baidu.com:8002
d. 路飞线上代码无跨域(项目部署时,放在同一处)
- vue.js
- 前端三大框架:
#react.js /angular.js / vue.js vue.js 2版本
- 组件:
#- axios #- vuex #- router
- 你以为vue和jQuery的区别?
#- 双向绑定 #- 单页面应用
内容详细:
1. redis字典
#- 安装redis,在内存中进行存取数据。 #- 启动redis服务 #redis-server
- 虚拟机问题:
#1. 网卡链接方式桥接 #2. iptables 关闭 #service iptables stop #3. 修改redis配置文件 #vim /etc/redis.conf
- 初始redis
a. redis至关因而一个在内存中建立的大字典。 b. redis的value有5大数据类型: #- 字符串 #import redis #conn = redis.Redis(host='192.168.11.61',port=6379) # 设置值 #conn.set('wupeiqi_name','于超') # 获取值 #val = conn.get('wupeiqi_name').decode('utf-8') #print(val) #- 列表 #- 集合 #- 有序集合 #- 字典
2. 购物车逻辑
问题:
a. 为何要把购物车信息放到redis中?
#- 查询频繁 #- 课程是否存在? #- 价格策略是否合法? #- 中间状态 #- 购买成功以后,须要删除。 #- 购物车信息删除
b. 购物车有没有数量限制?
#使用 keys 查看个数作判断
c. 购物车的结构
#redis = { #shopping_car_用户ID_课程ID:{ #id:'课程ID', #name:'课程名称', #img:'课程图片', #default_price_id:'默认价格ID', #price_policy_dict:{ #1: {...}, #5: {...}, #} #} #}
总结:
a. 五大数据类型
b. 列举每种数据类型的操做
#字符串: #set #get #字典: #get #hgetall #set #hmset #hdel #其余: #delete #expire #keys #flushall()