gpt4 book ai didi

python - django rest framework 没有身份验证没有权限

转载 作者:太空宇宙 更新时间:2023-11-04 07:18:12 27 4
gpt4 key购买 nike


我想在 django rest 框架中没有任何身份验证或许可的情况下访问 api。每当我进行以下设置时,为了实现这一点,我最终都会出错。

设置:

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (),
'DEFAULT_AUTHENTICATION_CLASSES': (),
'PAGE_SIZE': 10
}

错误:

TypeError at /endpoints/

'many' is an invalid keyword argument for this function

Request Method: GET
Request URL: http://localhost:9900/endponint/?format=api
Django Version: 1.8.3
Exception Type: TypeError
Exception Value:

'many' is an invalid keyword argument for this function

Exception Location: /home/user/.virtualenvs/test/lib/python3.4/site-packages/django/db/models/base.py in __init__, line 480
Python Executable: /home/user/.virtualenvs/test/bin/python
Python Version: 3.4.0
Python Path:

['/home/user/Projects/test/new',
'/home/user/.virtualenvs/test/lib/python3.4',
'/home/user/.virtualenvs/test/lib/python3.4/plat-x86_64-linux-gnu',
'/home/user/.virtualenvs/test/lib/python3.4/lib-dynload',
'/usr/lib/python3.4',
'/usr/lib/python3.4/plat-x86_64-linux-gnu',
'/home/user/.virtualenvs/test/lib/python3.4/site-packages']

Server time: Wed, 5 Aug 2015 13:46:35 +0000

Endpoint:


Request Method: GET
Request URL: http://localhost:9900/endpoint/?format=api

Django Version: 1.8.3
Python Version: 3.4.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'app')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')


Traceback:
File "/home/user/.virtualenvs/test/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/user/.virtualenvs/test/lib/python3.4/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/home/user/.virtualenvs/test/lib/python3.4/site-packages/rest_framework/viewsets.py" in view
85. return self.dispatch(request, *args, **kwargs)
File "/home/user/.virtualenvs/test/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
456. response = self.handle_exception(exc)
File "/home/user/.virtualenvs/test/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
453. response = handler(request, *args, **kwargs)
File "/home/user/.virtualenvs/test/lib/python3.4/site-packages/rest_framework/mixins.py" in list
43. serializer = self.get_serializer(page, many=True)
File "/home/user/.virtualenvs/test/lib/python3.4/site-packages/rest_framework/generics.py" in get_serializer
109. return serializer_class(*args, **kwargs)
File "/home/user/.virtualenvs/test/lib/python3.4/site-packages/django/db/models/base.py" in __init__
480. raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])

Exception Type: TypeError at /endpoints/
Exception Value: 'many' is an invalid keyword argument for this function

但是当我使用以下设置时它可以工作但要求身份验证凭据。

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
'DEFAULT_AUTHENTICATION_CLASSES': (),
'PAGE_SIZE': 10
}

请告诉我如何在没有任何凭据的情况下访问 rest api。

我正在运行 django 1.8 djangorestframework 3.1

最佳答案

您可以为 DJR 使用以下设置:

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

AllowAny许可并不是真正需要的,但我认为在配置中明确定义该行为是一种很好的做法。

关于python - django rest framework 没有身份验证没有权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31834476/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com