aabb
aabb

Reputation: 79

Django-allauth change redirect url when changing password

allauth redirects to login view when i change password. I need to change redirect url to index page. How to do it?

This is what i have so far:

views.py

def change_password(request, *args, **kwargs):
    return Http404

urls.py

url('^password/change/$', 'change_password', name='change_password'),

but it still redirects me to login page;/

Upvotes: 2

Views: 6406

Answers (4)

getup8
getup8

Reputation: 8228

Wanted to add a version where you need a dynamic URL and should instead override get_success_url().

views.py

from django.urls import include, path

from core.users import views as user_views

urlpatterns = [
    path('accounts/password/change/',
         user_views.CustomPasswordChangeView.as_view(),
         name='account_change_password'),
    path('accounts/', include('allauth.urls')),
]

core/views.py

from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse

from allauth.account.views import PasswordChangeView


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

hipertracker
hipertracker

Reputation: 2458

The working solution tested in django v1.11.21 and django-all-auth v0.39.1:

First:

url(r"^accounts/password/change/$", CustomPasswordChangeView.as_view(), name="account_password_change")

Second:

from allauth.account.views import PasswordChangeView

class CustomPasswordChangeView(PasswordChangeView):
    success_url = '/' # <- choose your URL

That's all.

Upvotes: 4

Thameem
Thameem

Reputation: 3636

inside your app views.py file, override the PasswordChangeView of django allauth

from django.core.urlresolvers import reverse_lazy
from allauth.account.views import PasswordChangeView


class LoginAfterPasswordChangeView(PasswordChangeView):
    @property
    def success_url(self):
        return reverse_lazy('generic:password_change_success')


login_after_password_change = login_required(LoginAfterPasswordChangeView.as_view())

in urls.py (where django allauth urls reside ). this url shoul above comes above this (url(r'^accounts/', include('allauth.urls')),) to override default change password behaviour

 url(r'^accounts/password/change/$', generic_views.login_after_password_change, name='account_change_password'), 

url(r'^accounts/', include('allauth.urls')),

this function for redirecting the after succefully change password. here you have to create a html page with this name "password_change_success.html" in account template folder

@login_required
def password_change_success(request):
    template = "account/password_change_success.html"
    return render(request, template)

in url.py

url(r'^password_change_success/$', views.password_change_success, name="password_change_success"),

Upvotes: 1

jph
jph

Reputation: 367

Try changing your views to:

def change_password(request, *args, **kwargs):
    return HttpResponseRedirect("/") //put the name of the url you want to change to within the quotes. As it stands, it will take you to, I'm assuming, what your homepage would be.

Make sure to import:

from django.shortcuts import render, HttpResponseRedirect

Upvotes: 0

Related Questions