Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is It possible to add permissions to specific url in Django

I am using IsAuthenticated permission by default and let's say I do not want to change the default permission. Is it possible to give permission of AllowAny to a specific URL?

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
]

Thanks in Advance

like image 441
Madiyor Avatar asked Sep 05 '25 23:09

Madiyor


1 Answers

include_docs_urls function has a parameter with a default value like this 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

the default behavior is to extend the value of DEFAULT_PERMISSION_CLASSES from you settings but you can override it like this

from rest_framework.permissions import AllowAny
urlpatterns = [
    path('docs/', include_docs_urls(title='Great Soft Uz', permission_classes=[AllowAny, ], authentication_classes=[])) 
]
like image 78
Mohamed Abd El-hameed Avatar answered Sep 08 '25 23:09

Mohamed Abd El-hameed