Django 跨域

先安装 pip install django-cors-headers

setting.py

在 INSTALLED_APPS 中 添加 'corsheaders',

在 MIDDLEWARE 中首行 添加

复制代码
'corsheaders.middleware.CorsMiddleware',

添加 CORS_ALLOW_ALL_ORIGINS =True

多条记录查询输出json格式

python 复制代码
from django.core import serializers



def departmentlist(request):
    department=models.Department.objects.all()
    json_data = serializers.serialize('json', department)  # 将查询结果进行json序列化
    return HttpResponse(json_data, content_type="application/json")  # 返回json数据