Получение {"detail": "Метод \"POST\" не разрешен."} для регистрации пользователя
Я знаю, что об этом спрашивали уже тысячу раз, но ни один из них не решил мой вопрос. Я пытаюсь использовать Rest в своем представлении. Я получаю ошибку, указанную в заголовке, когда отправляю запрос на пост к урлу accounts/register/.
models.py:
from django.contrib.auth.models import AbstractUser
from django.db import models
from .validators import phone_validator
class User(AbstractUser):
class Gender(models.TextChoices):
MALE = 'M', 'Male'
FEMALE = 'F', 'Female'
UNSET = 'MF', 'Unset'
phone = models.CharField(max_length=15, validators=[phone_validator], blank=True)
address = models.TextField(blank=True)
gender = models.CharField(max_length=2, choices=Gender.choices, default=Gender.UNSET)
age = models.PositiveSmallIntegerField(blank=True, null=True)
description = models.TextField(blank=True)
@property
def is_benefactor(self):
return hasattr(self, 'benefactor')
@property
def is_charity(self):
return hasattr(self, 'charity')
serializers.py:
class Meta:
model = User
fields = [
"username",
"password",
"address",
"gender",
"phone",
"age",
"description",
"first_name",
"last_name",
"email",
]
def create(self, validated_data):
user = User(**validated_data)
user.set_password(validated_data['password'])
user.save()
return user
urls.py (тот, что в приложении. он включен в корневой urls.py с урлом 'accounts/')
from django.urls import path
from rest_framework.authtoken.views import obtain_auth_token
from .views import UserRegistration, LogoutAPIView
urlpatterns = [
path('login/', obtain_auth_token),
path('logout/', LogoutAPIView.as_view()),
path('register/', UserRegistration.as_view()),
]
views.py:
from rest_framework import status
from rest_framework.mixins import CreateModelMixin
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView
from .serializers import UserSerializer
from .models import User
class LogoutAPIView(APIView):
permission_classes = (IsAuthenticated,)
def post(self, request):
request.user.auth_token.delete()
return Response(
data={'message': f'Bye {request.user.username}!'},
status=status.HTTP_204_NO_CONTENT
)
class UserRegistration(APIView,CreateModelMixin):
permission_classes = (AllowAny,)
serializer_class= UserSerializer
queryset=User.objects.all()
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
Что я делаю не так?