Reputation: 2441
I have a similar question to this -
Conditional login redirect in Django
But I couldn't understand how to achieve the result from answers there.
I am relatively new to django. I reused this code from somewhere, which redirects the user to login page. But after login I always get to the start/home page of the user. I want them to see the page they really requested, and not the user homepage all the time. Can you tell me what and where I can make the change, it should be where I am using 'redirect' function. I probably should save some session variable and do it but not quite getting the starting point. any thoughts?
Below is the code -
def view_or_basicauth(view, request, test_func, realm = "", *args, **kwargs):
if test_func(request.user): # Already logged in, just return the view.
return view(request, *args, **kwargs)
# They are not logged in. See if they provided login credentials
if 'HTTP_AUTHORIZATION' in request.META:
auth = request.META['HTTP_AUTHORIZATION'].split()
if len(auth) == 2:
# NOTE: We are only support basic authentication for now.
if auth[0].lower() == "basic":
uname, passwd = base64.b64decode(auth[1]).split(':')
user = authenticate(username=uname, password=passwd)
if user is not None:
if user.is_active:
login(request, user)
request.user = user
return view(request, *args, **kwargs)
# Either they did not provide an authorization header or something in the authorization attempt failed. Send a 401 back to them to ask them to authenticate.
key = request.path.split('/')
if len(key) > 1:
base_url = request.get_host()
return redirect( 'https://' + base_url + '/login/')
s = '401 Unauthorized'
response = HttpResponse(s)
response.status_code = 401
response['Content-Length'] = '%d' % len(s)
response['WWW-Authenticate'] = 'Basic realm="%s"' % realm
return response
Upvotes: 0
Views: 1758
Reputation: 118508
It's pretty straight forward:
Add a GET parameter to to your redirect line so that your login view knows where your user is coming from. It can be anything, but I'm using "?redirect=myurl"
.
In your login view: check for the existence of that key (redirect
) in GET upon successful login. If it exists, redirect to the value.
First modify your redirect line:
# add a GET parameter to your redirect line that is the current page
return redirect( 'https://' + base_url + '/login/?redirect=%s' % request.path )
Then in your login view, just check for your variable in GET and redirect to that value if it exists.
# modify the login view to redirect to the specified url
def login(request):
# login magic here.
if login_successful:
redirect = request.GET.get('redirect') # get url
if redirect:
# redirect if a url was specified
return http.HttpResponseRedirect(redirect)
# otherwise redirect to some default
return http.HttpResponseRedirect('/account/home/')
# ... etc
Upvotes: 1
Reputation: 799150
Pass the URL as a GET parameter in the redirect, and have the redirect target redirect them back after they've entered their credentials.
Upvotes: 0