【问题标题】:Django rest: You do not have permission to perform this action during creation api schemaDjango rest:您在创建 api 模式期间无权执行此操作
【发布时间】:2018-08-07 23:56:31
【问题描述】:

我正在尝试向我的 django 项目添加一些架构视图(我使用了 this 示例)

我的代码:

def get_auth():
    auth = [
        path('', include('rest_framework.urls', namespace='rest_framework')),
        path('register', RegisterApiView),
        path('token/obtain/', TokenObtainPairView),
        path('token/refresh/', TokenRefreshView),
    ]
    return auth 

def get_schema():
    schema_url_patterns = [
        path('api/auth', include(get_auth())),
    ]

    schema_view = get_schema_view(
        title='Auth Schema',
        url='/api/auth/',
        patterns=schema_url_patterns,
    )
    return schema_view


urlpatterns = [
  path('api/auth/', get_schema()),
]

当我尝试连接到 /api/auth/ 时遇到错误:

HTTP 403 Forbidden
Allow: GET, HEAD, OPTIONS
Content-Type: application/coreapi+json
Vary: Accept

{
    "detail": "You do not have permission to perform this action."
}

【问题讨论】:

    标签: python django django-rest-framework


    【解决方案1】:

    自己修复了,错误在get_auth() 方法中,我没有将as_view() 添加到view 类中:

    def get_auth():
        auth = [
            path('', include('rest_framework.urls', namespace='rest_framework')),
            path('register', RegisterApiView.as_view({'post': 'create'})),
            path('token/obtain/', TokenObtainPairView.as_view()),
            path('token/refresh/', TokenRefreshView.as_view()),
            ]
        return auth
    

    【讨论】:

      猜你喜欢
      • 2020-01-10
      • 2021-03-09
      • 2018-12-28
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 2019-08-07
      相关资源
      最近更新 更多