写代码:Django DRF修改admin 密码api
时间: 2024-01-20 17:04:33 浏览: 204
以下是Django DRF修改admin密码的API代码示例:
```python
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
@api_view(['PUT'])
def change_admin_password(request):
try:
user = User.objects.get(username='admin')
password = request.data.get('password')
user.password = make_password(password)
user.save()
return Response({'message': 'Admin password updated successfully'}, status=status.HTTP_200_OK)
except User.DoesNotExist:
return Response({'error': 'Admin user does not exist'}, status=status.HTTP_404_NOT_FOUND)
```
使用PUT请求访问该API,并将新密码作为请求体发送。请注意,此示例假定您的管理员用户名为“admin”。如果不是,请将其替换为您的管理员用户名。
阅读全文