我正在尝试实现 django-rest-frameworkjwt但我面临一个问题,如下图所示。我正在尝试遵循 django-restframeworkjwt 文档和 rest-auth documentation.
这里我使用自定义用户模型
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
最佳答案
在我看来,您错过了一件或可能的两件事,
rest-auth
url:url('^rest-auth/', include('rest_auth.urls'))
- 在设置中设置您的自定义用户:
AUTH_USER_MODEL = 'users.CustomUser'
(我看不到您的整个设置,您可能有这个点)
关于python - 如何正确使用 djangorestframworkjwt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60472983/