是否可以为 Django 中的特定 url 添加权限

Mad*_*yor 4 django django-rest-framework

我默认使用 IsAuthenticated 权限,假设我不想更改默认权限。是否可以向特定URL授予AllowAny权限?

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('user.urls')),
    path('api/section/', include('section.urls')),
    path('docs/', include_docs_urls(title='Great Soft Uz')) # I want this url to be public
]
Run Code Online (Sandbox Code Playgroud)

提前致谢

Moh*_*eed 5

include_docs_urls 函数有一个参数,其默认值如下:permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES

def include_docs_urls(
        title=None, description=None, schema_url=None, urlconf=None,
        public=True, patterns=None, generator_class=SchemaGenerator,
        authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
        permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES,
        renderer_classes=None):
# this is the declaration of the function
Run Code Online (Sandbox Code Playgroud)

默认行为是从您的设置中扩展 DEFAULT_PERMISSION_CLASSES 的值,但您可以像这样覆盖它

from rest_framework.permissions import AllowAny
urlpatterns = [
    path('docs/', include_docs_urls(title='Great Soft Uz', permission_classes=[AllowAny, ], authentication_classes=[])) 
]
Run Code Online (Sandbox Code Playgroud)