使用dj-rest-auth注册返回HTTP 204无内容,未生成Token问题求助
问题:dj-rest-auth注册返回204 No Content且不生成Token
我在学习William.S.Vincent所著的《Django for APIs》一书第8章「用户认证」时,采用dj-rest-auth和django-allauth实现Token认证的用户注册功能。书中注册成功会返回HTTP 201 Created状态码,创建新账户的同时生成并返回API认证Token,且Token会保存至数据库。但我的实现返回HTTP 204 No Content状态码,虽然账户能成功创建,却没有生成对应的Token。
我的urls.py代码
urlpatterns = [ path('admin/', admin.site.urls), path('api/v1/', include("posts.urls")), # v1 for api version 1. (Name for each api route) path('api-auth/', include('rest_framework.urls')), # build-in log in/out rest path("api/v1/dj-rest-auth/", include("dj_rest_auth.urls")), #url for dj_rest_auth path("api/v1/dj-rest-auth/registration/", include("dj_rest_auth.registration.urls")), ]
我的settings.py代码
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', #3party "rest_framework", "corsheaders", "rest_framework.authtoken", "allauth", "allauth.account", "allauth.socialaccount", "dj_rest_auth", "dj_rest_auth.registration", #local 'accounts.apps.AccountsConfig', 'posts.apps.PostsConfig',] REST_FRAMEWORK = { # new "DEFAULT_PERMISSION_CLASSES": [ "rest_framework.permissions.IsAuthenticated", ], "DEFAULT_AUTHENTICATION_CLASSES": [ "rest_framework.authentication.SessionAuthentication", "rest_framework.authentication.TokenAuthentication", ],}
我对比了作者提供的示例代码,未发现明显差异,怀疑是依赖版本变更导致的该问题,恳请得到帮助。
内容的提问来源于stack exchange,提问作者Dũng Nguyễn Văn
相关产品推荐
相关产品推荐

