3

I have a requirement that whenever I login or attempt to request a view that has a login_decorator then the next page be a page where I am required to ask the user to select a business entity (irreespective of the original view requested).

Let's say that the page is http://127.0.0.1:8999/APP/business_unit

To achieve this I configured the following in my settings.py

LOGIN_REDIRECT_URL='/APP/business_unit_selector'

Now when i try to access http://127.0.0.1:8999/APP/range_list the page goes to http://127.0.0.1:8999/APP/login?next=/APP/range_list I was expecting that the next page after login be /APP/business_unit

but instead, the next page was /APP/range_list

The browser address bar has http://127.0.0.1:8999/APP/login?next=/APP/range_list

Is it possible to achieve what I am trying in Django?

Guddu
  • 1,588
  • 4
  • 25
  • 53

2 Answers2

3

LOGIN_REDIRECT_URL is used unly when next is unspecified. In your test request there is next=/APP/range_list - and that address is used to redirect user after login.

Probably the easiest and most effective solution is to make your own decorator, similar to login_required which redirects to /APP/business_unit_selector&next=<redirect_url> if unit is not selected, and apply it together with login_required. It is not the most efficient solution in terms of redirects number, but is quite clean, and doesn't mess up the login page.

You will also have to handle next parameter in your business_unit_selector view, if you like to achieve natural flow.

Your decorator should be something like

from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
from django.utils.http import urlquote
import functools

def business_unit_required(view):
    @login_required  # probably you want to incorporate it here
    @functools.wraps(view)
    def wrapped(request, *args, **kwargs):
        if not 'selected_business_unit' in request.session:
            return redirect('%s?next=%s' % (
                reverse('business_unit_selector'),
                urlquote(request.get_full_path())))
        return view(request, *args, **kwargs)
    return wrapped
Frax
  • 5,015
  • 2
  • 17
  • 19
1

The reason that http://127.0.0.1:8999/APP/login?next=/APP/range_list is redirecting you to range_list after logging in, is because with next= you are overriding what is specified in your settings file, LOGIN_REDIRECT_URL='/APP/business_unit_selector'.

If I understand correctly you need to user to choose a business entity after logging in.

A couple solutions that come to mind are as follows:

1.) Don't use a separate forms for login and business entity. Instead combine them.

Username
Password
Business Entity

2.) You can also specify in your view if the user doesn't have a buisness entity ResponseRedirect("/APP/business_unit_selector") docs here

marcusshep
  • 1,916
  • 2
  • 18
  • 31
  • Thanks for your response. Corrected the post. When i try to access http://127.0.0.1:8999/APP/range_list the page goes to http://127.0.0.1:8999/APP/login?next=/APP/range_list I was expecting that the next page after login be /APP/business_unit – Guddu Mar 16 '15 at 18:04
  • Can you post the code for your view functions? I'm thinking that if I see that portion of the code I can get a better understanding of what's going on. – marcusshep Mar 16 '15 at 18:13