Opened 2 years ago

Closed 2 years ago

#19551 closed New feature (duplicate)

Enable dynamic redirect after login, by setting LOGIN_REDIRECT_URL to a callable

Reported by: gcc Owned by: nobody
Component: contrib.auth Version:
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It's quite a common requirement to redirect to a different page after login, for example depending on some information about the user, or the time of day, etc:

This involves:

  • either rewriting the login() view, which may be a security risk, as it's easy to get wrong, and definitely not DRY;
  • or doing a second redirect from the landing page after login which is inefficient and slow.

I think it would be a very simple change to allow LOGIN_REDIRECT_URL to point to a callable, in django.contrib.auth.views.login:

             if not redirect_to:
                 redirect_to = settings.LOGIN_REDIRECT_URL
+            if callable(redirect_to):
+                redirect_to = redirect_to(request, form.get_user())

Change History (1)

comment:1 Changed 2 years ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

I think the way to go is to transform the auth views in class-based views, then it will be easy to subclass and rewrite get_success_url. This is tracked in #17209.

Note: See TracTickets for help on using tickets.
Back to Top