allauth会重定向到登录视图。我需要将重定向url更改为索引页面。怎么做?
这是我到目前为止所做的:
views.py
def change_password(request, *args, **kwargs):
return Http404
urls.py
url('^password/change/$', 'change_password', name='change_password'),
但它仍然会将我重定向到登录页面; /
答案 0 :(得分:1)
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())
在urls.py中(django allauth urls所在的位置)。 上面的这个url高于这个(url(r'^ accounts /',include('allauth.urls')),)来覆盖默认的更改密码行为
url(r'^accounts/password/change/$', generic_views.login_after_password_change, name='account_change_password'),
url(r'^accounts/', include('allauth.urls')),
此功能用于重定向后成功更改密码。 在这里,你必须在帐户模板文件夹
中创建一个名为“password_change_success.html”的html页面@login_required
def password_change_success(request):
template = "account/password_change_success.html"
return render(request, template)
在url.py中
url(r'^password_change_success/$', views.password_change_success, name="password_change_success"),
答案 1 :(得分:0)
尝试将您的观点更改为:
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.
确保导入:
from django.shortcuts import render, HttpResponseRedirect
答案 2 :(得分:0)
在django v1.11.21
和django-all-auth v0.39.1
中经过测试的有效解决方案:
第一:
url(r"^accounts/password/change/$", CustomPasswordChangeView.as_view(), name="account_password_change")
第二:
from allauth.account.views import PasswordChangeView
class CustomPasswordChangeView(PasswordChangeView):
success_url = '/' # <- choose your URL
仅此而已。
答案 3 :(得分:0)
想添加一个需要动态URL的版本,而应覆盖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