Tyler Bell
Tyler Bell

Reputation: 897

Django allauth Redirect on Password Change Success

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!

Upvotes: 0

Views: 1801

Answers (1)

getup8
getup8

Reputation: 8228

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

Upvotes: 0

Related Questions