python - 如何正确使用 djangorestframworkjwt

标签 python django django-rest-framework

我正在尝试实现 django-rest-frameworkjwt但我面临一个问题,如下图所示。我正在尝试遵循 django-restframeworkjwt 文档和 rest-auth documentation. enter image description here

这里我使用自定义用户模型

from .managers import CustomUserManager


class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), unique=True)
    username = models.CharField(_("Username"), max_length=50, unique=True)
    ..........

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username', ]

    objects = CustomUserManager()

    def __str__(self):
        return self.email

    def get_full_name(self):
        return self.userprofile.first_name + " " + self.userprofile.last_name

    def get_short_name(self):
        return self.userprofile.first_name


class UserProfile(models.Model):
    user = models.OneToOneField(User, verbose_name=_("User"), on_delete=models.CASCADE)
    first_name = models.CharField(_("First name"), max_length=50)
   ....................

url.py

urlpatterns = [
    url(r'^api-token-auth/', obtain_jwt_token),
    url(r'^api-token-refresh/', refresh_jwt_token),
    url(r'^api-token-verify/', verify_jwt_token),
    path('api/v1/', include('apps.urls.v_1')),
]

和settings.py

# Application definition

TENANT_MODEL = "tenant.Client"

SHARED_APPS = [
    "tenant_schemas",
    "apps.tenant"
]

TENANT_APPS = [
.........
    "rest_framework",
    'rest_framework.authtoken',
    'rest_auth',
    "apps.users",
]

INSTALLED_APPS = SHARED_APPS + TENANT_APPS


REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        # 'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        # 'rest_framework.authentication.TokenAuthentication',
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    ),
}
REST_USE_JWT = True

我是不是做错了什么?这里我使用了三个包 djangorestframeworks、rest-auth、djangorestframworkjwt。 我正在使用

rest-auth and django restframworkjwt

最佳答案

在我看来,您错过了一件或可能的两件事,

  1. rest-auth url:url('^rest-auth/', include('rest_auth.urls'))
  2. 在设置中设置您的自定义用户:AUTH_USER_MODEL = 'users.CustomUser'(我看不到您的整个设置,您可能有这个点)

关于python - 如何正确使用 djangorestframworkjwt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60472983/

相关文章:

mysql - 如何以良好的方式将多个 django 表单保存到数据库中?提高绩效

python - django 'QuerySet' 对象中的反向关系没有属性 'name'

python - Django 全局名称 'PageNotAnInteger' 未定义

Django 在单独的数据库上测试给出 "OperationalError: no such table: auth_user"

python - python : …“none” type, expected integer

python - 使用 Deque.popleft 作为函数的参数

Python json 添加多个键并迭代

Django 2.1 查看权限

python - 我如何在 TensorFlow 中使用我自己的图像?

DjangoRestFramework ModelSerializer DateTimeField仅在创建对象时转换为当前时区