简体   繁体   English

Django-rest-framework权限

[英]Django-rest-framework permissions

I have UserList 我有用户列表

from rest_framework import permissions

class UserList(generics.ListCreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = (permissions.IsAuthenticated,)

As you see I need access only to authenticated users, but I always have all permissions. 如您所见,我仅需要访问经过身份验证的用户,但我始终拥有所有权限。 Any idea? 任何想法?

The code looks fine. 代码看起来不错。 There are a few things you can try: 您可以尝试以下几种方法:

  1. As @Druhin mentioned, ensure you've attempted the request in a new incognito window to ensure you don't have a cookie or something authenticating you. 如@Druhin所述,请确保您已在新的隐身窗口中尝试了该请求,以确保您没有Cookie或对您进行身份验证的内容。
  2. Ensure you are reaching the correct URL. 确保您到达正确的URL。 Are you sure you don't have another view mapped to that specific URL? 您确定没有其他视图映射到该特定URL吗?
  3. As an even more solid method, use CURL to send the request. 作为一种更可靠的方法,请使用CURL发送请求。
  4. This is not your problem, but you can also configure default permissions for the rest framework. 这不是您的问题,但是您也可以为其余框架配置默认权限。

For example, in settings.py: 例如,在settings.py中:

 REST_FRAMEWORK = {
      'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
     )
 }

If none of those options work, please post your settings and any other relevant code. 如果没有这些选项的工作,请发表您的设置和其他相关代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM