想要在django里面跨域的话,首先得有一个更换域名的操作
首先先进入
hosts文件里面
最后的三个是要改的域名
想要在网站访问的时候没有端口号只有域名的话
运行的时候 python manage.py runserver 80
加个80 就可以了
跨域操作的时候还得再setting文件里配置一下
#配置可跨域范围 CORS_ALLOW_CREDENTIALS = True CORS_ORIGIN_ALLOW_ALL = True
然后再INSTALLED_APPS里面添加跨域
INSTALLED_APPS = [ \'django.contrib.admin\', \'django.contrib.auth\', \'django.contrib.contenttypes\', \'django.contrib.sessions\', \'django.contrib.messages\', \'django.contrib.staticfiles\', \'mymac\', \'myapp\', \'myapp1\', #跨域 \'corsheaders\', #异步 \'djcelery\' ]
MIDDLEWARE = [ \'django.middleware.security.SecurityMiddleware\', \'django.contrib.sessions.middleware.SessionMiddleware\', #定义跨域中间件 \'corsheaders.middleware.CorsMiddleware\', \'django.middleware.common.CommonMiddleware\', #\'django.middleware.csrf.CsrfViewMiddleware\', \'django.contrib.auth.middleware.AuthenticationMiddleware\', \'django.contrib.messages.middleware.MessageMiddleware\', \'django.middleware.clickjacking.XFrameOptionsMiddleware\', ]
然后再中间件里添加个跨域中间件就行了
这样就可以跨域访问啦