Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic login_redirect_url in Django allauth

I'm using django-alluth for social login. My concern is how do I login to a specific url instead of the fixed LOGIN_REDIRECT_URL in the settings.py file?

When I try removing the LOGIN_REDIRECT_URL and setting my login page url as accounts/login/?next=/some-url/ it doesn't work. It redirects to accounts/profile which is a 404 in my code.

like image 694
Newtt Avatar asked Feb 04 '15 08:02

Newtt


2 Answers

You need to override get_login_redirect_url in DefaultAccountAdapter.

# project/settings.py:
ACCOUNT_ADAPTER = 'project.users.adapter.MyAccountAdapter'

# project/users/adapter.py:
from django.conf import settings
from allauth.account.adapter import DefaultAccountAdapter

class MyAccountAdapter(DefaultAccountAdapter):

    def get_login_redirect_url(self, request):
        path = "/accounts/{username}/"
        return path.format(username=request.user.username)

docs

like image 50
Khaino Avatar answered Nov 02 '22 04:11

Khaino


LOGIN_REDIRECT_URL = '/afterlogin'
ACCOUNT_AUTHENTICATED_LOGIN_REDIRECTS = True

This works for me...

like image 21
cwhisperer Avatar answered Nov 02 '22 04:11

cwhisperer