urls.py 1.1 KB

12345678910111213141516171819202122232425
  1. from django.urls import path
  2. from .views import UserRegistrationView, UserProfileView # 导入我们刚创建的视图
  3. from rest_framework_simplejwt.views import (
  4. TokenObtainPairView, # SimpleJWT 自带的视图,用于登录并获取token对
  5. TokenRefreshView, # SimpleJWT 自带的视图,用于刷新access token
  6. )
  7. app_name = 'accounts' # 为这个应用的URL模式设置一个命名空间 (可选,但推荐)
  8. urlpatterns = [
  9. # 用户注册
  10. path('register/', UserRegistrationView.as_view(), name='user_register'),
  11. # 用户登录 (使用SimpleJWT提供的视图)
  12. # 访问此URL并POST email和password,会返回access和refresh tokens
  13. path('login/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
  14. # 刷新Access Token (使用SimpleJWT提供的视图)
  15. # 访问此URL并POST refresh token,会返回新的access token
  16. path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
  17. # 用户个人资料 (获取和更新)
  18. # 需要认证 (在视图中已设置 IsAuthenticated)
  19. path('profile/', UserProfileView.as_view(), name='user_profile'),
  20. ]