contenttype组件、Django缓存机制以及跨域请求

 

1 昨日回顾
版本控制 ***
(1)url=127.0.0.1/course/?version=v100000
1 versioning_class=QueryParameterVersioning
'VERSION_PARAM':'version',
'DEFAULT_VERSION':'v2',
'ALLOWED_VERSIONS':['v1','v2']

2 配置成全局:在setting里:QueryParameterVersioning

(2)重要(以后建议用这种):127.0.0.1/v1/course/
1 路由改写:(?P<version>[v1|v2|v3]+)/version/
2 versioning_class=URLPathVersioning
'VERSION_PARAM':'version',
'DEFAULT_VERSION':'v2',
'ALLOWED_VERSIONS':['v1','v2']

2 配置成全局:再setting里:URLPathVersioning
(其他)1 v1.baidu.com/course/
2 放在头里
3 名称空间的

分页 ***
1 简单分页
PageNumberPagination
1 再试图类里 生成一个PageNumberPagination对象
2 对象.属性=xxx:修改属性
3 ret=models.Book.objects.all()
data_list=对象.paginate_queryset(ret,request,self)
4 序列化 instance,反序列化:data
5 my_page.get_paginated_response(ser.data)

2 偏移分页
就第二部不一样

3 加密分页
就第二部不一样:ordering

响应(渲染)器 *
局部:JSONRenderer,BrowsableAPIRenderer
全局:配置在settings里的rest_framework

2 路飞学城项目分析
-主站
课程系列:
-所有课程(专题课,学位课)
-课程详情
-视频播放
-课程章节
-课程评论
-课程价格策略
-加入购物车
-支付中心
-去支付(调支付宝支付)
首页
轻课
个人中心:
登陆
注册
个人中心

-管理员后台

-导师后台

3 ContentType组件
解决什么问题:表的id和数据id,来唯一确定一条数据
用:
插入数据:
models:content_obj = GenericForeignKey('table_id', 'course_id')
存:models.PricePolicy.objects.create(period=5, price=18000, content_obj=degree)

查询数据:
1 通过课程查价格策略:
policy = GenericRelation('PricePolicy', object_id_field='course_id', content_type_field='table_id')
course.policy.all() 拿出所有价格策略
2 通过价格策略查课程
content_obj = GenericForeignKey('table_id', 'course_id')
price.content_obj 拿到的就是课程对象

 

models层

 1 from django.db import models
 2 
 3 from django.contrib.contenttypes.models import ContentType
 4 from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
 5 
 6 
 7 class Course(models.Model):
 8     title = models.CharField(max_length=32)
 9     # 不会在数据库中生成字段,只用于数据库操作
10     # policy = GenericRelation('PricePolicy',object_id_field='object_id',content_type_field='contentType')
11 
12 
13 class DegreeCourse(models.Model):
14     title = models.CharField(max_length=32)
15 
16 
17 class PricePolicy(models.Model):
18     # 跟ContentType表做外键关联
19     contentType = models.ForeignKey(to=ContentType)
20     # 正数
21     object_id = models.PositiveIntegerField()
22 
23     # 引入一个字段,不会在数据库中创建,只用来做数据库操作
24     # content_obj = GenericForeignKey('contentType', 'object_id')
25 
26     period = models.CharField(max_length=32)
27     price = models.FloatField()

 

views层

 1 from app01 import models
 2 def test(request):
 3     import json
 4     # 方式一插入价格规则
 5     # ret=models.ContentType.objects.filter(model='course').first()
 6     # course=models.Course.objects.filter(pk=1).first()
 7     # print(ret.id)
 8     # models.PricePolicy.objects.create(period='30',price=100,object_id=course.id,contentType_id=ret.id)
 9 
10     # 方式二插入价格规则
11     # course=models.Course.objects.filter(pk=1).first()
12     # # content_obj=course  会自动的把课程id放到object_id上,并且去ContentType表中查询课程表的id,放到contentType上
13     # models.PricePolicy.objects.create(period='60',price=800,content_obj=course)
14     # 增加学位课,价格规则
15     # degreecourse = models.DegreeCourse.objects.filter(pk=1).first()
16     # models.PricePolicy.objects.create(period='60', price=800, content_obj=degreecourse)
17 
18     # 查询所有价格策略,并且显示对应的课程名称
19     # ret=models.PricePolicy.objects.all()
20     # for i in ret:
21     #     print(i.price)
22     #     print(i.period)
23     #     # content_obj 就是代指关联的课程,或者学位课程的那个对象
24     #     print(type(i.content_obj))
25     #     print(i.content_obj.title)
26 
27     # 通过课程id,获取课程信息和价格策略
28     course=models.Course.objects.filter(pk=1).first()
29     print(course.policy.all())
30 
31 
32 
33 
34     return render(request,'test.html')

 

4 Django缓存机制
三个粒度:有粗粒度和细粒度,这里不做研究

缓存是放在最外层的中间件里,第一次请求经过多个中间件过滤,请求到数据,返回的时候数据缓存到最外层,再次请求可以直接在最外层拿到(缓存有时间限制)

1 全站缓存 在中间件做缓存 全局缓存是所有的类和函数都要遵守规定的。

用中间件: 注意:这里第一个中间件是处理请求过来的信息request,最后一个中间件是处理response。
MIDDLEWARE = [
# 'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.security.SecurityMiddleware',
。。。。。。
# 'django.middleware.cache.FetchFromCacheMiddleware'

]
# CACHE_MIDDLEWARE_SECONDS=10  在settings里设置缓存失效时间。在设置的时间内缓存是一直存在的。
2 单视图:
用装饰器
from django.views.decorators.cache import cache_page
# @cache_page(24*60*60)  导入cache_page 传入缓存时间,只有加了装饰器的才会有缓存。有缓存的在一段时间数据是不会变化的,没缓存的数据实时变化。
3 局部页面:  这个一般是前端处理,在前端页面导入cache模块,{%cache 5 'test'%} 这里的cache是一个函数,需要传参。
{% load cache %}
{% cache 5 'test' %} 两个参数:5是时间,test是唯一标识 
{{ ctime }}
{% endcache %}

 

5 跨域请求
1 浏览器的同源策略

同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略基础之上的,浏览器只是针对同源策略的一种实现

请求的url地址,必须与浏览器上的url地址处于同域上,也就是域名,端口,协议相同.

请求的信息来到服务器,服务器做出响应,只是响应来到浏览器被浏览器拒绝。


2 简单请求:发一次请求
response['Access-Control-Allow-Origin'] = '*'   * 代表所有的访问ip+port来源
response['Access-Control-Allow-Origin'] = 'http://127.0.0.1:8008,http://127.0.0.1:8009'
2 非简单请求:发两次,一次预检(OPTIONS),第二次是真正的请求
if request.method=='OPTIONS':  通过一个简单的判断
response['Access-Control-Allow-Methods']='PUT,DELETE'
response['Access-Control-Allow-Headers']='Content-Type'
建议写在中间件里
注释:里面尽量不要写(*),都支持(*)  这里尽量不要写* 虽然都支持,默认规定。

跨域请求写在中间件里:定义后记得加在中间件里。需要写中间件要注意一点:不会写的时候可以仿照中间件源码。

 

 1 from django.utils.deprecation import MiddlewareMixin
 2 
 3 class MyMiddle(MiddlewareMixin):
 4     def process_response(self, request, response):
 5         if request.method=='OPTIONS':
 6             response['Access-Control-Allow-Methods']='*'
 7             response['Access-Control-Allow-Headers']='*'
 8 
 9         response['Access-Control-Allow-Origin'] = '*'
10         return response

 

 

 

作业:https://www.cnblogs.com/liuqingzheng/articles/9794285.html 

 

posted @ 2018-10-24 20:45  Roc_Atlantis  阅读(188)  评论(0编辑  收藏  举报