用户登录
- 模型类需要继承 AbstractUser
from django.contrib.auth.models import AbstractUser
from django.utils import timezone
class User(AbstractUser):
mobile = models.CharField('手机号', max_length=11)
last_login = models.DateTimeField('上次登录时间', default=timezone.now)
def __str__(self):
return self.username
class Meta:
db_table = 'user'
- 还有配置CRUD
AUTH_USER_MODEL = 'users.User
- 需要安装 Djangorestframework-jwt
pip install Djangorestframework-jwt
- 配置路由里面继承 obtain_jwt_token
from rest_framework_jwt.views import obtain_jwt_token
urlpatterns = [
path('login/', obtain_jwt_token),
]
- 还需要添加数据密码还得是bash64编码,可以直接在导航栏里输入一下代码
python manage.py shell
>>>from users.mobile import User
>>>User.objects.create_user(username='zhangsan',password=