You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用rest_auth重置密码时遇post()意外参数'uidb64'错误求助

解决rest_auth密码重置POST请求时的post() got an unexpected keyword argument 'uidb64'错误

嘿,这个问题我之前也碰到过,根源其实是URL路径里捕获的参数没被视图的post方法正确接收处理,咱们一步步来修复:

错误原因拆解

你在URL配置里把uidb64和token作为路径参数捕获了:

re_path(r'^password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', PasswordResetConfirmView.as_view(), name='password_reset_confirm')

当用户提交重置密码的POST请求时,Django会自动把这两个参数作为关键字参数传给视图的post方法,但你定义的post方法只接收request这一个参数,完全没处理这些额外的kwargs,所以就抛出了那个错误。

两种修复方案

方案一:修改视图的post方法,适配URL参数

调整你的PasswordResetConfirmView的post方法,让它接收额外的关键字参数,并且把这些参数注入到请求数据里,供序列化器使用:

class PasswordResetConfirmView(GenericAPIView):
    serializer_class = PasswordResetConfirmSerializer
    permission_classes = (AllowAny,)

    @sensitive_post_parameters_m
    def dispatch(self, *args, **kwargs):
        return super(PasswordResetConfirmView, self).dispatch(*args, **kwargs)

    # 给post方法加上kwargs参数
    def post(self, request, *args, **kwargs):
        # 把URL里的uidb64和token转成序列化器需要的字段名
        request.data['uid'] = kwargs.get('uidb64')
        request.data['token'] = kwargs.get('token')
        
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response({"detail": _("Password has been reset with the new password.")})

这样修改后,URL里的参数就能被序列化器正确读取,符合PasswordResetConfirmSerializer的参数要求。

方案二:改用rest_auth默认的URL配置(更推荐)

rest_auth本身已经内置了密码重置确认的端点,不需要自己搞路径参数。你可以把自定义的URL替换成默认配置:

# 先导入默认视图
from rest_auth.views import PasswordResetConfirmView

urlpatterns = [
    # ... 其他URL配置
    # 替换成默认的密码重置确认端点
    path('password/reset/confirm/', PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
]

这种情况下,前端需要把uid和token放在POST请求的请求体(body)里,而不是URL路径中,这也是rest_auth原生的设计逻辑,能避免很多参数传递的问题。

额外小提醒

你的URL配置里有个重复路径的坑:

path('registration/', include('rest_auth.registration.urls')),
path('registration/', RegisterView.as_view(), name='account_signup'),

后面的registration/会覆盖前面的include配置,导致rest_auth注册相关的其他端点无法访问。建议把自定义注册视图的路径改成path('registration/signup/', RegisterView.as_view(), name='account_signup'),,避免冲突。

内容的提问来源于stack exchange,提问作者Fady Alfred

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:42:26