【问题标题】:How to authenticate valid super user or not in django如何在 django 中验证有效的超级用户
【发布时间】:2018-06-16 01:42:31
【问题描述】:

我在 Django 中创建了一个超级用户。有什么方法可以使用 Postman 中的 REST API 验证超级用户凭据?还是我需要编写脚本化的 REST API?

如果我的问题过于宽泛,请告诉我。我会更新我的问题。

问候

【问题讨论】:

    标签: python django django-rest-framework django-rest-auth


    【解决方案1】:

    就我个人而言,我使用 django-rest-framework http://www.django-rest-framework.org/,它有一个名为 django-rest-auth http://django-rest-auth.readthedocs.io/en/latest/ 的第三方模块,它提供 API 端点来处理登录、注册和其他用户访问。该文档非常好,并且已经存在了很长时间。

    django.contrib.auth 用户模型具有布尔值 is_staff 和 is_superuser,并且有一个装饰器来检查人员 https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#the-staff-member-required-decorator

    文档中的示例

    from django.contrib.admin.views.decorators import staff_member_required
    
    @staff_member_required
    def my_view(request):
    

    您可以通过实现以下风格的类似内容来轻松创建自己的装饰器:

    def super_user_required(view_func=None, 
                              redirect_field_name=REDIRECT_FIELD_NAME,
                              login_url='admin:login'):
        """
        Decorator for views that checks that the user is logged in and is a superuser
        member, redirecting to the login page if necessary.
        """
        actual_decorator = user_passes_test(
            lambda u: u.is_active and u.is_superuser,
            login_url=login_url,
            redirect_field_name=redirect_field_name
        )
        if view_func:
            return actual_decorator(view_func)
        return actual_decorator
    

    该软件包提供可选的 JWT 支持。 https://getblimp.github.io/django-rest-framework-jwt/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多