0

I am developing a web-app which will support multiple customers. All customers will be housed in the same database. An authentication group will be created for each customer, permissions will be assigned at the group level to ensure that only database resources pertaining to that particular customer can be accessed. Users will be bound to the group for their company and permissions overrides (if desired) will be handled at this level.

Each customer will be able to log into Django Admin, again only having access to database resources for their company. On the front end, each customer will have their own URL. For example:

http://127.0.0.1:8000/customer1
http://127.0.0.1:8000/customer2

When a customer accesses their front-end URL directly, I want to force them to log in. I want to use the @login_required decorator to redirect them to the Django Admin login page and upon successful authentication I want to pass the user through to the URL they requested in the first place. For example

* User puts 'http://127.0.0.1:8000/customer1' into their browser
* Browser redirects to authentication page
* User provides credentials and authenticates successfully
* User is passed through to 'http://127.0.0.1:8000/customer1'

Currently I believe that I have things configured correctly (I'm probably wrong else I wouldn't be posting this) but the decorator seems to not be called/invoked/functioning properly. When the user inputs http://127.0.0.1:8000/customer1 URL into their browser, they go straight to the URL as before (page loads successfully) but with no redirect occurring for the desired authentication behavior.

ROOT URL FILE (\DJANGO_PROJECT\DJANGO_PROJECT\urls.py):

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'', include('basesite.urls', namespace='basesite')),
    url(r'^admin/', admin.site.urls),
    url(r'^customer1/', include('customer1.urls', namespace='customer1')),
    url(r'^customer2/', include('customer2.urls', namespace='customer2')),
]

CUSTOMER1 URL FILE (\DJANGO_PROJECT\customer1\urls.py):

from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from customer1.models import Company

@login_required(login_url='admin/')
def index(request):
    companies = Company.objects.all()
    context = { 'companies': companies }
    return render(request, 'customer1/index.html', context)

Thanks in advance for any assistance with this problem.

Dan
  • 331
  • 6
  • 17

1 Answers1

0

Disregard, I found the answer on another SO post:

django - How to redirect django.contrib.auth.views.login after login?

It seems I was missing entries from my root urls.py file and also perhaps was unclear that I needed to use integrated views and create template.html files. I had conceptualized that I would just use the admin interface to log in but maybe that's not possible, not sure.

Community
  • 1
  • 1
Dan
  • 331
  • 6
  • 17