Django-tenants and domain routing issue

Hello, I’m a newbie to django multitenancy. I successfully implemented multit-tenancy in my app (using django-tenants using shared db seperated schema approach).

Problem

What I’m trying to implement

I’m trying to create a portal akin to the one shown here for users to login:
https://letmein.blackboard.com/router

I have a problem in implementing a portal where users can login by clicking a dropdown menu to select active tenants in my login page for my multitenant app.

My problem is the domain url for the created tenants is not properly displaying in my login page.

I have tried creating another page to test if it’s a problem with my auth system and the list of tenants display properly but upon clicking on listed tenants I get a None response instead of tenant.localhost.com.

Here is a sample code snippet from my login page.

#login.html template

<input
            type="password"
            name="password"
            id="password_id"
            required
            placeholder="Password"
          />
          <select class="form-control" name="center" id="centerSelect" onchange="redirectToCenter()">
            <option value="http://{{ tenant.domain_url}}">--Select Your Center--</option>
            {% for tenant in tenants %}
                <option value="http://{{ tenant.domain_url}}">{{ tenant.center_name }}</option>
                {% empty %}
                <option value="http://{{ tenant.domain_url}}">No centers available</option>
            {% endfor %}
        </select>r paste code here

Front-end

I don’t know where the issue seem to lie.

So, in conclusion I’m having problem displaying the list of tenants in my login page and not getting redirected to the correct domain url too.

Any help in solving this would be greatly appreciated

Thanks

What is your view that is rendering your login page? Is it supplying the list of tenants for the template in the context?

I’m not sure I understand what you meant.

I am using from django.contrib.auth.decorators import login_required to redirect user to the login page.

this is the view I have

#accounts/api/views.py

from rest_framework import generics
from django.contrib.auth import get_user_model

from .serializers import UserSerializer


class UserListAPIView(generics.ListAPIView):
    lookup_field = "id"
    serializer_class = UserSerializer

    def get_queryset(self):
        queryset = get_user_model().objects.all()
        query = self.request.GET.get("q")
        if query is not None:
            queryset = queryset.filter(username__iexact=q)
        return queryset


class UserDetailView(generics.RetrieveAPIView):
    User = get_user_model()
    lookup_field = "id"
    queryset = User.objects.all()
    model = User

Don’t think in terms of pages, think in terms of views.

The login_required decorator is going to redirect the browser to a url.

This url is going to cause a view to be executed, to create an html page.

That view is responsible for populating the context for the template being rendered in that view.

If you’re going to reference a variable named tenants in your template, then the view rendering that template needs to provide that value in the context.

That view you’re showing here does not appear to be the one creating the login page.

Thank you. I’m slowly understanding what you mean.

Here’s my main app config in the screenshot below. Could you please direct to the page you asking? is it the one highlighted in blue?

I am using django default auth login and logout views as shown here (please correct me if wrong)

Where would I need to reference the variable named tenants in this scenario?

You would need to replace that default view with your own view.

How would I override the default view?

Here’s my current view for accounts

#account.view.py