Django:重定向更改密码URL [英] Django: Redirect Change Password URL

查看:70
本文介绍了Django:重定向更改密码URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个URL:

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:

I thought if I used the following within the change password form it would override what URL would be loaded:

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

但是当我从<$ c到达更改密码时$ c> url(r'^ change-passwordiOS / $'并单击更改按钮,它不会按预期进入/ profileiOS /,而是进入标准/ profile / URL。

But when I reach the change password from the url(r'^change-passwordiOS/$' and click the "Change Button" it does not goto the /profileiOS/ as expected but the standard /profile/ URL.

任何帮助将不胜感激。

/更改密码/视图:

@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>


推荐答案

您正在手动重定向到 / profile / password_changed 视图中。您可以更改重定向逻辑以依赖于模板中的发布参数:

You are manually redirecting to /profile/ in password_changed view. You can change your redirect logic to depend on post param from template:

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

这篇关于Django:重定向更改密码URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆