这是我的做法:
from django.test import Clientimport base64auth_headers = { 'HTTP_AUTHORIZATION': 'Basic ' + base64.b64enpre('username:password'),}c = Client()response = c.get('/my-protected-url/', **auth_headers)注意:您还需要创建一个用户。

这是我的做法:
from django.test import Clientimport base64auth_headers = { 'HTTP_AUTHORIZATION': 'Basic ' + base64.b64enpre('username:password'),}c = Client()response = c.get('/my-protected-url/', **auth_headers)注意:您还需要创建一个用户。