Redirect User to another url with django-allauth log in signal

Shiva Krishna Bavandla picture Shiva Krishna Bavandla · Nov 22, 2013 · Viewed 13.5k times · Source

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 in settings.py file

LOGIN_REDIRECT_URL = '/thanks/'

But when the user tried to log in for the next time(if already registered) I should redirect him to '/dashboard/' URL

So 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, my AccountAdapter class looks like below

from 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?

Answer

pennersr picture pennersr · Nov 22, 2013

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/'