【发布时间】:2022-08-04 20:59:44
【问题描述】:
我有一个关于 Django Rest Framework JWT 身份验证协议的问题。
这个问题已经出现了很多,但没有建议的解决方案对我有用。
当我尝试这个命令时:
http post http://127.0.0.1:8000/api/token/ username=username password=password
或者
curl -X POST -d \"username=username&password=password\" http://localhost:8000/api/token/
要获得许多教程中建议的访问/刷新令牌,我收到此错误:
{ \"detail\": \"没有找到具有给定凭据的活动帐户\" }
我创建了一个超级用户
我的用户都是 is_active = True
我的密码在数据库中散列
我在 settings.py 中有 AUTH_USER_MODEL = \'my_app_name.User\'
用户名/密码是 100% 正确的。
这是我的用户模型:
class User(LifecycleModelMixin, AbstractUser): public_id = models.UUIDField(unique=True, default=uuid.uuid4, editable=False) company_name = models.CharField(max_length=100, blank=True) job_title = models.CharField(max_length=30, blank=True) street_address = models.CharField(max_length=100, blank=True) street_address2 = models.CharField( verbose_name=\"Street address 2\", max_length=100, blank=True ) city = models.CharField(max_length=100, blank=True) state = models.CharField(max_length=50, blank=True) zip = models.CharField(max_length=50, blank=True) phone_number = PhoneNumberField(blank=True) is_active = models.BooleanField(default=True, null=True, blank=True) email_subscribed = models.BooleanField(default=True, null=True, blank=True) manager = models.ForeignKey( \"self\", null=True, blank=True, on_delete=models.SET_NULL, related_name=\"sub_users\", ) country = CountryField(blank_label=\"(select country)\", blank=True) contact_info = JSONField(\"ContactInfo\", default=contact_default)我的序列化器:
class UserSerializer(serializers.ModelSerializer): def create(self, validated_data): user = super().create(validated_data) user.set_password(validated_data[\'password\']) user.save() return user class Meta: model = User fields = (\'email\', \'username\', \'refreshToken\', \'password\') extra_kwargs = {\'password\': {\'write_only\': True}}我的网址:
from rest_framework_simplejwt.views import ( TokenObtainPairView, TokenRefreshView, TokenVerifyView, ) urlpatterns: List[URLPattern] = ( [ path(\'api/token/\', TokenObtainPairView.as_view(), name=\'token_obtain_pair\'), path(\'api/token/refresh/\', TokenRefreshView.as_view(), name=\'token_refresh\'), path(\'api/token/verify/\', TokenVerifyView.as_view(), name=\'token_verify\'), path(\'api-token-auth/\', obtain_auth_token, name=\'api_token_auth\'), path(\"auth/\", include(\"authapp.urls\")), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) )我的设置.py:
DJANGO_APPS = [ \"django.contrib.admin\", \"django.contrib.auth\", \"django.contrib.contenttypes\", \"django.contrib.sessions\", \"django.contrib.messages\", \"django.contrib.staticfiles\", \"django.contrib.sites\", \"django.forms\", ] INSTALLED_APPS = [ # styling \"crispy_forms\", \"crispy_tailwind\", \"crispy_bootstrap5\", \"widget_tweaks\", # rest framework \'rest_framework\', \'rest_framework.authtoken\', #celery \"django_celery_beat\", # dev \"django_extensions\", \"debug_toolbar\", # deploy \"whitenoise.runserver_nostatic\", # auth \'authapp\', \'rest_framework_simplejwt\', \'djoser\', \"allauth\", \"allauth.account\", \"allauth.socialaccount\", # mail \"anymail\", # utils \"phonenumber_field\", \"simple_history\", \"markdownify\", ] REST_FRAMEWORK = { \'DEFAULT_AUTHENTICATION_CLASSES\': ( \'rest_framework_simplejwt.authentication.JWTAuthentication\', \'rest_framework.authentication.SessionAuthentication\', \'rest_framework.authentication.BasicAuthentication\', ), \'DEFAULT_PERMISSION_CLASSES\':( \'rest_framework.permissions.IsAuthenticated\', ) }我不知道为什么会这样。这是我第一次使用 Django Rest Framework,因此我怀疑我可能忘记添加一些重要的东西。
-
请包括您的
INSTALLED_APPS。 -
我已经用我安装的应用程序编辑了我的配置的 settings.py 部分。
标签: python django django-models django-rest-framework jwt