【发布时间】:2021-11-03 20:53:15
【问题描述】:
我正在使用 Django 3.2 和 django-allauth 0.44
我有一个自定义适配器,可以在登录时将用户重定向到他们的个人资料页面 - 但是,我还希望能够使用 ?next=/some/url/(例如 /accounts/login/?next=https://127.0.0.1:8000/&page=2),以便用户在存在时被重定向到该 url是GET 参数中的next 参数。
这是我的自定义适配器(根据@iklinac 的建议进行了修改):
class MyAccountAdapter(DefaultAccountAdapter):
def get_next_redirect_url(self, request, redirect_field_name="next"):
"""
Returns the next URL to redirect to, if it was explicitly passed
via the request.
"""
redirect_to = get_request_param(request, redirect_field_name)
if not self.is_safe_url(redirect_to):
redirect_to = None
return redirect_to
def get_login_redirect_url(self, request, url=None, redirect_field_name="next", signup=False):
ret = url
if url and callable(url):
# In order to be able to pass url getters around that depend
# on e.g. the authenticated state.
ret = url()
if not ret:
ret = self.get_next_redirect_url(request, redirect_field_name=redirect_field_name)
if not ret:
if signup:
#ret = self.get_signup_redirect_url(request)
return reverse('profile', kwargs={'username': request.user.username})
else:
ret = '/' # Go home ..
return ret
我不得不修改我的自定义适配器,以便它在进入配置文件页面之前检查“下一个”GET 参数(如果没有指定 next 参数)。
但是,我的代码中的逻辑以某种方式被忽略了(next=/some/url 参数仍然被忽略)。
如何在使用自定义适配器的同时将 ?next=/some/url 与 django-allauth 一起使用?
【问题讨论】:
-
@iklinac 谢谢。感谢您提供的链接,我已经更新了我的问题。
-
@HomunculusReticulli 看来您正在遵循我之前给您的解决方案,据我所知它应该可以工作。以下是一些调试信息:
allauth.account.views.LoginView在其form_valid方法中调用allauth.account.forms.LoginForm的login方法,然后调用allauth.account.utils.perform_login,然后调用allauth.account.utils.get_login_redirect_url,最后调用适配器get_login_redirect_url。可能的问题是您没有为适配器设置ACCOUNT_ADAPTER设置,或者您正在使用一些自定义登录视图。 -
这篇文章中适配器的初始版本也足够了(到
next的重定向由allauth.account.utils.get_login_redirect_url处理。 -
@HomunculusReticulli 我会在一段时间内写一个答案。
标签: django django-authentication django-allauth