I have a view defined for a url ‘site/main/’. I would like to be able to have (unauthenticated) users redirected to the default ‘/admin/’ page for login, then redirected to the ‘/main/’ page after successful login. I followed the django documentation, but I must be missing something as I am unable to get this to work.
My view looks like:
def main(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('admin/?next=%s' % request.path)
else:
I get an error:
Page not found (404)
Request Method: GET
Request URL:http://sitename:8080/main/admin/?next=/main/
Any help is greatly appreciated !
You’re missing an initial
/in the URL:/admin/?next=...However this still won’t work, as the
adminURL doesn’t know anything about thenextparameter. That’s only for the actual login views. With your code, the user will be logged into the admin but will not be redirected back to your page.You should build a login template and wire it up to the built-in login views. Then instead of checking
is_authenticatedin the view, you should just use thelogin_requireddecorator.