Django drf未提供身份验证凭据

Django drf未提供身份验证凭据,django,python-3.x,django-rest-framework,Django,Python 3.x,Django Rest Framework,当我尝试访问api时,出现以下错误: "detail": "Authentication credentials were not provided." 我已将其包含在settings.py中: REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES':( 'rest_framework.authentication.TokenAuthentication', 'rest_framework.authentication.Sess

当我尝试访问api时,出现以下错误:

 "detail": "Authentication credentials were not provided."
我已将其包含在settings.py中:

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES':(
    'rest_framework.authentication.TokenAuthentication',
    'rest_framework.authentication.SessionAuthentication',


),
'DEFAULT_PERMISSION_CLASSES':(
    'rest_framework.permissions.IsAuthenticated',
)

}
我的应用程序api URL.py:

from django.urls import path,include
from . import views
from rest_framework import routers

router = routers.SimpleRouter()
router.register(r'',views.UserViewSet, 'user_list')
urlpatterns = router.urls
my views.py:

class UserViewSet(viewsets.ModelViewSet):
       queryset = User.object.all()
       serializer_class = serializers.UserSerializers
serializers.py:

from rest_framework import serializers
from users.models import User


class UserSerializers(serializers.ModelSerializer):
  class Meta:
    model = User
    fields = ('email','password')
my main URL.py:

urlpatterns = [
path('admin/', admin.site.urls),
path('',include(urls)),
path ('', include(user_urls)),
path('api/',include(api_urls)),
当我运行localhost:8000/api时,我得到了一个错误

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES':(
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES':(
        'rest_framework.permissions.IsAuthenticated',
    )
}

在您的
settings.py
文件中使用它,发生的情况是
rest\u framework.authentication.TokenAuthentication
需要一个带有token作为其值的授权头,但您不能用浏览器发送它,要从浏览器浏览API,必须启用会话身份验证。

如果使用令牌身份验证,则无法从浏览器url访问API。 正如@DarkOrbTokenAuthentication所说,身份验证需要一个带有token作为其值的授权头。 因此,无论何时调用api,都必须传递令牌。 您可以使用来测试您的api

在上图中,我在postman的头中传递了令牌以访问我的api。 当您从前端调用api时,将令牌和请求一起传递。
如果您只想在桌面浏览器中使用api,那么您可以仅使用会话身份验证。对于移动设备,必须执行令牌身份验证。

能否显示我在edits@afk你登录了吗?理想情况下,使用此代码,您应该能够在postman和browser中同时使用API。另外,顺序也很重要,首先进行会话身份验证会给您带来邮递员发出的csrf问题。您做错了什么?知道它为什么不起作用以及是什么使它起作用是很重要的。