使用Django REST API,我试图验证我的请求.

这就是我想要传达的信息:

Content-Type: application/json, Authentication: token="6d82549b48a8b079f618ee9c51a6dfb59c7e2196"

这是我得到的回报:

{"detail": "Authentication credentials were not provided."}

有人能给我正确的标题吗?

谢谢

标题:

Accept: application/json
Content-Type: application/json
Authorization: Token 6d82549b48a8b079f618ee9c51a6dfb59c7e2196
Connection: keep-alive
Origin: chrome-extension: //rest-console-id
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17

在此处输入图像描述

设置.py

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.permissions.IsAdminUser',


    ),
    'PAGINATE_BY': 10
}

view.py

class ProfileList(generics.ListCreateAPIView):
    """
    API endpoint that represents a list of users.
    """
    permission_classes = (permissions.IsAuthenticated,)
    model = Profile
    serializer_class = ProfileSerializer

    def pre_save(self, obj):
        obj.owner = self.request.user

推荐答案

假设您正在try 使用TokenAuthentication,头应该如下所示:

Authorization: Token 6d82549b48a8b079f618ee9c51a6dfb59c7e2196

如所描述的in the documentation.

Django相关问答推荐

Django REST framework:object has no attributed after annotation;Got attributeError when try to get a value for field field on serializer<>

Django www.example.com从常量列表中删除值

如何删除Docker上的django应用程序?

Django:如何 Select 没有其他项目引用的项目?

如何在 Django 中将下一个 8 月 1 日添加为 DateTimeField 默认值

Django中的 联合(Union) 和相交(Intersect)

如何使用 matplotlib 在绘图的角落插入小图像?

ModelForm 的 Django TextField max_length 验证

Django:无法从另一个应用程序导入模型

Django 自定义管理器 - 如何仅返回登录用户创建的对象?

django.request 记录器没有传播到根目录?

Heroku - 在 Django 应用程序中处理静态文件

测试 Django 信号的正确方法

__init__() 得到了一个意外的关键字参数user

带有 Django 和 Python 的 Atlassian Bamboo - 可能吗?

Django 国际化语言代码

Django ORM 能否以可靠的与后端无关的方式存储无符号 64 位整数(又名 ulong64 或 uint64)?

模拟 Django 查询集以测试采用查询集的函数

如何在 Django 中使用动态外键?

django/文件上传权限