如何配置django-allauth实现用户登录后的落地页跳转
It looks like your redirect is breaking because you’re using a relative path for LOGIN_REDIRECT_URL, which gets appended to the current login URL instead of pointing to the root. Here’s how to fix it, along with best practices:
Root Cause
When you set LOGIN_REDIRECT_URL='survey/' (without a leading slash), Django treats this as a relative path. Since the login page lives at /accounts/login/, the redirect becomes /accounts/login/survey/—a non-existent route, hence the 404 error.
Solutions
1. Use an Absolute Path (Quick Fix)
Update your settings.py to use an absolute path with a leading slash:
LOGIN_REDIRECT_URL = '/survey/'
This tells Django to redirect directly to the root-level /survey/ URL instead of appending to the current login path.
2. Use a Named URL Pattern (Best Practice)
Hardcoding URL paths is fragile if you ever change your URL structure. Instead, use the named URL from your survey namespace:
- First, check your
survey/urls.pyto find the name of your root survey path. For example:from django.urls import path from . import views urlpatterns = [ path('', views.survey_home, name='survey_home'), # Root path with a name # Other survey URLs... ] - Then update
settings.pyto use the namespace-qualified name:LOGIN_REDIRECT_URL = 'survey:survey_home'
This uses Django’s URL reversing system to dynamically generate the correct URL, even if you later change the path from /survey/ to something else.
3. Use Django-AllAuth’s Specific Setting
Django-allauth provides its own setting that takes precedence over the default LOGIN_REDIRECT_URL. If you want to explicitly configure allauth’s redirect, use:
ACCOUNT_LOGIN_REDIRECT_URL = '/survey/' # Or the named URL like 'survey:survey_home'
This is useful if you have other authentication systems in your project that might use the default LOGIN_REDIRECT_URL.
Additional Checks
- Restart Your Server: After updating settings, always restart your Django development server to apply the changes.
- Check for 'next' Parameter: If your login template includes a hidden
nextfield, make sure it’s not set to an incorrect value. If you want to use the default redirect, remove or omit this field from the login form.
内容的提问来源于stack exchange,提问作者Riconami

