Django:重定向更改密码URL

时间:2017-07-18 21:01:36

标签: django django-forms

我有以下两个网址:

url(r'^change-password/$',django.contrib.auth.views.password_change,{'template_name': 'meta/changepassword.html', 'post_change_redirect': '/password-changed/'},name='change_password'),
url(r'^change-passwordiOS/$',django.contrib.auth.views.password_change,{'template_name': 'meta/changepassword.html', 'post_change_redirect': '/password-changed/'},name='change_passwordiOS'),

我想如果我在更改密码表单中使用了以下内容,它将覆盖将加载的URL:

        {% if 'iOS' in request.path %}
          <input type="hidden" name="next" value="/profileiOS/" />
        {% endif %}

但是当我从url(r'^change-passwordiOS/$'到达更改密码并点击&#34;更改按钮&#34;它没有按预期转到/ profileiOS /,而是转移到标准/配置文件/ URL。

任何帮助都将不胜感激。

/ change-password / view:

@login_required
def password_changed(request):
    messages.success(request, 'Your password has been changed.')
    return redirect(reverse('profile'))

完整更改密码表格:

      <form class="form-horizontal" role="form" method="post" action="">
        {% csrf_token %}
        <fieldset>
          <div class="form-group">
            <label class="col-md-6 control-label">{{ form.old_password.label }}:</label>
            <div class="col-md-6">
              <input name="old_password" type="password" class="form-control"/>
              <div class="text-danger">
                {% for error in form.old_password.errors %}{{ error }}<br/>{% endfor %}
              </div>
            </div>
          </div>
          <div class="form-group">
            <label class="col-md-6 control-label">{{ form.new_password1.label }}:</label>
            <div class="col-md-6">
              <input name="new_password1" type="password" class="form-control"/>
              <div class="text-danger">
                {% for error in form.new_password1.errors %}{{ error }}<br/>{% endfor %}
              </div>
            </div>
          </div>
          <div class="form-group">
            <label class="col-md-6 control-label">{{ form.new_password2.label }}:</label>
            <div class="col-md-6">
              <input name="new_password2" type="password" class="form-control"/>
              <div class="text-danger">
                {% for error in form.new_password2.errors %}{{ error }}<br/>{% endfor %}
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="text-right col-sm-12">
              <button type="submit" class="btn btn-primary">Change Password^</button>
            </div>
          </div>
        </fieldset>
        {% if 'iOS' in request.path %}
          <input type="hidden" name="next" value="/profileiOS/" />
        {% endif %}
      </form>

1 个答案:

答案 0 :(得分:1)

您正在/profile/视图中手动重定向到password_changed。您可以将重定向逻辑更改为依赖于模板中的post param:

@login_required
def password_changed(request):
  messages.success(request, 'Your password has been changed.')
  return redirect(request.POST.get('next', reverse('profile')))