1

我正在尝试使用 django-rest-framework 在我的项目中实现 djorse,但是在尝试创建或删除用户时,端点不起作用,因为它们没有配置。

我该如何解决?

我已经将 django 设置文件配置为集成 djoser,但它仍然无法正常工作。

设置.py

DJOSER = {
    "ACTIVATION_URL": "activate/{uid}/{token}/",
    "PASSWORD_RESET_CONFIRM_URL": urllib.parse.urljoin(
        WEB_APP_URL, "/password/reset/confirm/{uid}/{token}"
    ),
    "EMAIL": {
        "activation": "users.emails.ActivationEmail",
        "confirmation": "djoser.email.ConfirmationEmail",
        "password_reset": "users.emails.PasswordResetEmail",
    },
    "PERMISSIONS": {
        "activation": ["rest_framework.permissions.AllowAny"],
        "password_reset": ["rest_framework.permissions.AllowAny"],
        "password_reset_confirm": ["rest_framework.permissions.AllowAny"],
        "set_password": ["djoser.permissions.CurrentUserOrAdmin"],
        "set_username": ["rest_framework.permissions.IsAuthenticated"],
        "user_create": ["rest_framework.permissions.AllowAny"],
        "user_delete": ["rest_framework.permissions.IsAdminUser"],
        "user": ["djoser.permissions.CurrentUserOrAdminOrReadOnly"],
        "user_list": ["crm.permissions.IsAdminOrCoach"],
        "token_create": ["rest_framework.permissions.AllowAny"],
        "token_destroy": ["rest_framework.permissions.IsAuthenticated"],
    },
    "SEND_ACTIVATION_EMAIL": True,
    "SET_PASSWORD_RETYPE": True,
    "SERIALIZERS": {
        "current_user": "auth.serializers.CurrentUserSerializer",
        "user": "auth.serializers.CurrentUserSerializer",
        "user_create": "users.api.serializers.UserRegistrationSerializer",
    },
}

用户/序列化程序.py

class UserRegistrationSerializer(UserCreateSerializer):
    email = serializers.EmailField(
        max_length=200,
        validators=[
            UniqueValidator(
                queryset=User.objects.all(), message=USER_ALREADY_EXISTS_ERROR_MESSAGE
            )
        ],
    )

    class Meta(UserCreateSerializer.Meta):
        pass

网址.py

urlpatterns += [
    path("crm/", include("crm.urls", namespace="crm")),
    path("workouts/", include("workouts.urls", namespace="workouts")),
    path("api/v1/auth/", include("auth.urls", namespace="auth-api")),
    path("api/v1/accounts/", include("djoser.urls")),
    path(
        "api/v1/accounts/users/activate/<str:uid>/<str:token>/",
        ActivateAccountView.as_view(),
        name="activate-user-account",
    ),
    path(
        "activation-failed",
        ActivateAccountFailed.as_view(),
        name="activate-user-account-failed",
    ),
    path("api/v1/", include("crm.api.urls", namespace="crm-api")),
    path("api/v1/", include("workouts.api.urls", namespace="workouts-api")),
    path("api/v1/documentation/", schema_view.with_ui("swagger", cache_timeout=0)),
]

这是错误,当我向端点 api/v1/accounts/users/create {POST} 请求

{
    "detail": "Method \ "POST \" not allowed."
}
4

0 回答 0