I found painful issue when LOGIN_URL contains query params.
For example in my settings.py a have:
LOGIN_URL = '/accounts/?signin'
When I try accessing to private page I got redirect like /accounts/?signin?next=/path_to_private_page/
Small code changes in cms/views.py solves the problem:
93. tup = settings.LOGIN_URL , "next", path
94. return HttpResponseRedirect('%s?%s=%s' % tup)
93. parse_res = list(urlparse(settings.LOGIN_URL))
94. params = parse_qsl(parse_res, True) # 4 is query string
95. params.append(('next', path))
96. parse_res = urlencode(params)
97. url = unquote(urlunparse(parse_res))
98. return HttpResponseRedirect(url)
after this changes redirect url is /accounts/?signin=&next=/path_to_private_page/
Django's auth views handle the same problem in a almost exactly the same way: see trunk of redirect_to_login, so the proposed fix should be fairly battle tested by now.
Edit: in fact, why not just call out to contrib.auth.views.redirect_to_login()? That view doesn't seem especially tied to the default auth implementation.
okay, better solution when using django 1.3.1:
93. return redirect_to_login(path, settings.LOGIN_URL)
but not sure that other django versions have the exactly the same code in redirect_to_login func.
Add test for #1208
Fixed in #2039
More resilient test for #1208
Fix testfor #1208
Backport from 3.0 of a better test for #1208