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

Django自定义用户模型密码重置确认页get_context_data缺失user参数报错

问题根因

你的自定义ResetPasswordForm构造方法要求必须传入user作为必填参数,但你在UserPasswordResetView的get_context_data方法中手动实例化表单时没有传入该参数,同时还覆盖了父类PasswordResetConfirmView已经生成好的、带正确参数的表单实例,因此触发参数缺失报错。

修复方案

直接删除你手动添加的表单实例化代码即可,父类已经自动帮你生成了符合要求的表单,你只需要额外注入你需要的静态文本变量:
修改后的views.py代码如下:

class UserPasswordResetView(auth_views.PasswordResetConfirmView):
    template_name = '../frontend/templates/frontend/templates.user/template.page_passwordReset.html'
    form_class = ResetPasswordForm
    post_reset_login = True
    success_url = reverse_lazy('page_userLoginPrivate')

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['user'] = self.user
        return kwargs

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # 不需要手动覆盖form,父类已经生成好正确实例
        if self.validlink:
            context.update({
                'validlink': True,
                'static_json_text': static_textLanguage,
                'static_json_textGlobal': static_textGlobal
            })
        else:
            context.update({
                'validlink': False,
                'form': None,
                'title': _('Password reset unsuccessful'),
                'static_json_text': static_textLanguage, 
                'static_json_textGlobal': static_textGlobal
            })
        return context

额外注意事项

你的模板文件中存在一个小问题:第二个密码框的错误提示仍然判断的是key == 'new_password1',会导致第二个密码框的错误无法正常显示,修改对应判断即可:

<div id="page_userPasswordReset_input_passwordB_alert_errors" class="alert_message_input_danger_format">
    {% if form.errors %}
        {% for key, value in form.errors.items %}
            {% if key == 'new_password2' %}
                {{ value }}
            {% endif %}
        {% endfor %}
    {% endif %}                                
</div>

内容的提问来源于stack exchange,提问作者Marcos Benvenuto

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 02:30:02