Django: Redirect Change Password URL

北城以北 提交于 2019-12-25 10:57:28

问题


I have the following two URLs:

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'),

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 %}

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.

Any help would be appreciated.

/change-password/ view:

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

Full Change Password Form:

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


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')))


来源:https://stackoverflow.com/questions/45176991/django-redirect-change-password-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!