How to get Custom Adapter to respect the 'next' URL query parameter
I am using allauth in my Django project and I have set up a custom account adapter which I have specified in settings.py
- ACCOUNT_ADAPTER
. My issue is once the user logs in in at a URL like /accounts/login/?next=/checkout/, my Custom Adapter has a get_login_redirect_url
method defined to redirect users based on their user type. The thing is I would like this adapter to respect the next
URL parameter. But when I look at the values - request.GET
, request.POST
, request.session
, the next
parameter is blank or empty.
How to achieve this?
The reason next is empty in request.GET and request.POST is that Django Allauth doesn’t keep it there during the login process. Instead, it saves the next URL in the user’s session under the key 'account_login_next'.
So, to get the redirect URL inside your custom adapter, you just need to check request.session like
from allauth.account.adapter import DefaultAccountAdapter
from django.urls import reverse
class CustomAccountAdapter(DefaultAccountAdapter):
def get_login_redirect_url(self, request):
next_url = request.session.get('account_login_next', '')
if next_url:
# Optional: remove it so it doesn’t get reused accidentally
del request.session['account_login_next']
return next_url
if request.user.is_staff:
return reverse('dashboard')
return reverse('home')
Basically, Allauth grabs the next parameter from the URL when you first visit the login page and puts it in the session so it can use it later after login. That’s why you don’t see it in GET or POST anymore by the time your adapter runs.