跨域
浏览器的同源策略
-- 阻止ajax请求不阻止src请求
JsonP 解决跨域的方式
添加响应头
-- 简单请求和复杂请求
简单请求满足:
HTTP方法是下列方法之一
HEAD, GET,POST
HTTP头信息不超出以下几种字段
Accept, Accept-Language, Content-Language, Last-Event-ID
Content-Type只能是下列类型中的一个
application/x-www-from-urlencoded
multipart/form-data
text/plain
-- 复杂请求
-- 先发送预检请求 OPTIONS
-- 写一个中间件
class MyCors(MiddlewareMixin):
def process_response(self, request, response):
response["Access-Control-Allow-Origin"] = "*"
if request.method == "OPTIONS":
# 复杂请求会先发预检
response["Access-Control-Allow-Headers"] = "Content-Type"
response["Access-Control-Allow-Methods"] = "PUT,PATCH,DELETE"
return response
ContentType组件
-- 一张表跟多张表创建外键关系的时候
-- content_type = models.ForeignKey(to=ContentType)
到ContentType 定位到表
object_id = models.IntegerField()
# 定位到对象id
content_object = GenericForeignKey("content_type", "object_id")
# 不会生成字段 只用于关联到对象的