0

This question has been brought up before here: https://github.com/pennersr/django-allauth/issues/468

It's closed and is a few years old, which could explain why its not working for me. I am simply trying to redirect to a different page other than the change password page after the password is successfully changed.

Here is my code, which is not making the page redirect on success.

#ursl.py
url(r'accounts/password/change', views.custom_password_change),
url(r'^accounts/', include('allauth.urls'))
...

#views.py
from allauth.account.views import PasswordChangeView
from django.contrib.auth.decorators import login_required

class CustomPasswordChangeView(PasswordChangeView):
    print("Getting Here")
    @property
    def success_url(self):
        print('Inside Success')
        return '/unknown/'

custom_password_change = login_required(CustomPasswordChangeView.as_view())

After submitting a password change, my terminal is printing "Getting Here" so it is definitely getting to that custom view. But its not printing "Inside Success".

Any help is appreciated! Thanks!

Tyler Bell
  • 837
  • 10
  • 30
  • Note that "Getting Here" is printed when the module is loaded and the class is defined, not when the view runs. – Alasdair May 25 '17 at 15:58
  • You are missing the trailing slash in the regex - `r'^accounts/password/change/$'` would be better. You have missed out `name='account_change_password'` as well. I don't think that will fix the problem though. – Alasdair May 25 '17 at 16:00
  • I made the changes you suggested but same result. – Tyler Bell May 25 '17 at 16:13
  • Have you been able to solve this issue? I have exactly the same isssue. – caliph Nov 27 '18 at 21:11

1 Answers1

0

success_url is a property, not a method. So you can either do:

class CustomPasswordChangeView(PasswordChangeView):
    success_url = '/unknown/'

Or, if you need dynamic URLs (say, with access to User), you can override get_success_url(), a class method that basically just returns the success_url property. An example of that below.

class CustomPasswordChangeView(LoginRequiredMixin, PasswordChangeView):
    """
    Overriding Allauth view so we can redirect to profile home.
    """
    def get_success_url(self):
        """
        Return the URL to redirect to after processing a valid form.

        Using this instead of just defining the success_url attribute
        because our url has a dynamic element.
        """
        success_url = reverse('users:user-detail',
                              kwargs={'username': self.request.user.username})
        return success_url
getup8
  • 6,949
  • 1
  • 27
  • 31