将用户重定向到具有django-allauth登录信号的另一个URL [英] Redirect User to another url with django-allauth log in signal
问题描述
我正在使用Django-allauth进行登录/注册相关内容,所以当用户首次登录到我的网站时,我将他重定向到 / thanks /
页面通过在 settings.py
文件中定义以下设置
LOGIN_REDIRECT_URL ='/ thanks /'
但是当用户下次尝试登录时(如果已经注册),我应该重定向到'/ dashboard /'
URL
所以尝试用 Django-allauth信号
/ p>
@receiver(allauth.account.signals.user_logged_in)
def registered_user_login(发送者,** kwargs):
instance = User.objects.get_by_natural_key(kwargs ['user'])
print instance.last_login == instance.date_joined,???????????? ????????????
如果不是instance.last_login == instance.date_joined:
return HttpResponseRedirect(reverse('dashboard'))
任何人都可以让我知道如何将用户重定向到 / dashboard /
进行正常登录,我在上面做错了什么信号码?
修改
根据以下答案进行了一些修改后 pennersr
,我的 AccountAdapter
类看起来像下面
<$ p $来自allauth.account.adapter import的p>
DefaultAccountAdapter
#from django.contrib.auth.models import User
class AccountAdapter(DefaultAccountAdapter):
def get_login_redirect_url(self,request):
if request.user.last_login == request.user.date_joined:
return'/ registration / success /'
else:
return'/ dashboard /'
但是,它仍然将用户重定向到
一般来说,您不应该尝试将这样的逻辑放在信号处理程序中。如果有多个处理程序想要转向不同的方向?
相反,请执行以下操作:
#settings.py:
pre>
ACCOUNT_ADAPTER ='project.users.allauth.AccountAdapter'
#project / users / allauth.py:
class AccountAdapter(DefaultAccountAdapter):
def get_login_redirect_url(self,request):
return'/ some / url /'
I am using Django-allauth for my login/signup related stuff, so when a user signs up(first time) into my site, I am redirecting him to
/thanks/
page by defining below setting insettings.py
fileLOGIN_REDIRECT_URL = '/thanks/'
But when the user tried to log in for the next time(if already registered) I should redirect him to
'/dashboard/'
URLSo tried to alter that with
Django-allauth signals
like below which is not working at all@receiver(allauth.account.signals.user_logged_in) def registered_user_login(sender, **kwargs): instance = User.objects.get_by_natural_key(kwargs['user']) print instance.last_login==instance.date_joined,"??????????????????????????????" if not instance.last_login==instance.date_joined: return HttpResponseRedirect(reverse('dashboard'))
So can anyone please let me know how to redirect a user to
/dashboard/
for the normal login, am I doing anything wrong in the above signal code?Edit
After some modification according to the below answer by
pennersr
, myAccountAdapter
class looks like belowfrom allauth.account.adapter import DefaultAccountAdapter # from django.contrib.auth.models import User class AccountAdapter(DefaultAccountAdapter): def get_login_redirect_url(self, request): if request.user.last_login == request.user.date_joined: return '/registration/success/' else: return '/dashboard/'
But still, it is redirecting the user to
/dashboard/
, my logic in determining the first time user is wrong?解决方案In general, you should not try to put such logic in a signal handler. What if there are multiple handlers that want to steer in different directions?
Instead, do this:
# settings.py: ACCOUNT_ADAPTER = 'project.users.allauth.AccountAdapter' # project/users/allauth.py: class AccountAdapter(DefaultAccountAdapter): def get_login_redirect_url(self, request): return '/some/url/'
这篇关于将用户重定向到具有django-allauth登录信号的另一个URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!