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

自定义password_reset_confirm.html模板无法跳转至password_reset_complete视图

解决自定义password_reset_confirm.html提交后不重定向的问题

嘿,我看你遇到的这个重定向问题,大概率和表单的提交逻辑、字段命名或者AngularJS的拦截行为有关,给你梳理几个核心排查点和解决办法:

  • 先检查表单的action和提交行为
    你的表单action=""是空的,虽然Django默认会处理当前URL的POST请求,但如果AngularJS接管了表单提交,就会用AJAX发送请求而不是触发浏览器默认的页面跳转。要是想保留Django原生的重定向逻辑,要么让Angular别拦截表单的默认提交,要么在AJAX请求成功后手动跳转。

    比如,如果你只是需要Angular做密码显示切换这类交互,不需要它处理提交,可以这样调整:

    <form id="reset-pw-confirm-form" name="newPWForm" method="post" action="{% url 'password_reset_confirm' uidb64=uid token=token %}">
      {% csrf_token %}
      <!-- 你的密码输入框 -->
      <button type="submit">提交新密码</button>
    </form>
    
  • 确认表单字段的name属性完全正确
    你代码里的输入框name显示为new_passwo...,看起来被截断了。Django的密码重置表单要求两个密码字段的name必须是new_password1和new_password2,如果名字不对,视图接收不到正确的密码数据,就只会停留在当前页面,不会触发重定向。一定要保证字段name和Django内置表单的字段名完全匹配:

    <input id="id_new_password1" type="[[[ newPW.showPW ? 'text' : 'password' ]]]" name="new_password1" ...>
    <input id="id_new_password2" type="[[[ newPW.showPW ? 'text' : 'password' ]]]" name="new_password2" ...>
    
  • 如果要保留Angular处理,手动控制提交和跳转
    要是你需要保留Angular的交互功能,就得自己写提交逻辑,在请求成功后手动跳转到password_reset_complete页面。可以用Angular的$http发送POST请求,然后通过$window跳转:

    angular.module('app', [])
      .controller('Ctrl', function($scope, $http, $window) {
        $scope.submitResetForm = function() {
          var formEl = document.getElementById('reset-pw-confirm-form');
          var formData = new FormData(formEl);
          
          $http.post('', formData, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
          }).then(function() {
            // 提交成功后跳转到完成页面
            $window.location.href = "{% url 'password_reset_complete' %}";
          }, function(error) {
            // 这里可以处理表单验证失败的情况,比如提示用户密码不匹配
          });
        };
      });
    

    然后给表单加上ng-submit指令:

    <form id="reset-pw-confirm-form" name="newPWForm" method="post" action="" ng-submit="submitResetForm()">
      {% csrf_token %}
      <!-- 你的字段 -->
      <button type="submit">提交</button>
    </form>
    
  • 最后检查Django的URL配置
    确保你的urls.py里已经正确映射了password_reset_complete视图,默认的Django auth视图会自动处理重定向,但如果是自定义了视图,要确认处理成功后返回的是redirect('password_reset_complete')。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:39:31