如何移除Django rest auth中的/rest-auth/user/端点?
/rest-auth/user/ Endpoint While Keeping Other rest-auth Functionality Hey there! To get rid of the auto-generated /rest-auth/user/ endpoint (the GET/PUT/PATCH one) while keeping login, registration, password management, etc., you just need to avoid using include("rest_auth.urls") directly — instead, manually add only the specific rest-auth views you want to keep. Here's how to adjust your urls.py:
Step 1: Import the required rest-auth views
First, import the individual views from rest_auth that you want to retain (skip UserDetailsView since that's the one powering the /user/ endpoint you don't need).
Step 2: Manually define each rest-auth URL
Replace the generic include("rest_auth.urls") line with explicit paths for login, logout, password change/reset, etc.
Modified urls.py Code
from django.urls import path # Import only the rest-auth views you need from rest_auth.views import ( LoginView, LogoutView, PasswordChangeView, PasswordResetView, PasswordResetConfirmView ) from . import views # Assuming your UserProfileView is in this app's views urlpatterns = [ # Manual rest-auth endpoints (no /user/ included) path('login/', LoginView.as_view(), name='rest_login'), path('logout/', LogoutView.as_view(), name='rest_logout'), path('password/change/', PasswordChangeView.as_view(), name='rest_password_change'), path('password/reset/', PasswordResetView.as_view(), name='rest_password_reset'), path('password/reset/confirm/', PasswordResetConfirmView.as_view(), name='rest_password_reset_confirm'), # Your existing registration endpoint path('register/', include('rest_auth.registration.urls'), name="user-auth-registration"), # Your custom user profile endpoint path('<uid>/', views.UserProfileView.as_view(), name="user-profile"), ]
Why this works
The default rest_auth.urls includes all endpoints, including UserDetailsView mapped to /user/. By explicitly adding only the views you need, you exclude the /user/ endpoint entirely while keeping all other rest-auth functionality intact.
This approach gives you full control over which endpoints are exposed, so you can keep using your custom UserProfileView without any conflicts.
内容的提问来源于stack exchange,提问作者Nithin

