requests请求django接口跨域问题处理

参考:
https://zhuanlan.zhihu.com/p/416978320
https://blog.csdn.net/SweetHeartHuaZai/article/details/130983179

使用httpx代替requests

import httpx

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
}

res = httpx.get(url='https://www.baidu.com/', headers=headers, timeout=10, verify=False)
print(res.text)

django中添加如下配置:

INSTALLED_APPS = [

'corsheaders',

]

MIDDLEWARE_CLASSES = [

'corsheaders.middleware.CorsMiddleware', #这个放到第一位

]

注意:
是MIDDLEWARE_CLASSES 不是MIDDLEWARE,否则不生效。

你可能感兴趣的:(django,python,后端)