Separate URL's for Multitenant app using sites framework

#1

Hi everyone,

I’m new to Django and I’m using the sites framework to create a multitenant application. Each tenant would have their own subdomain and siteid

customer1.mydomain.com
customer2.mydomain.com
etc

Now I want to setup a couple of pages on the main domain such as a static about page and a signup page that will ask several questions and then setup the customer by creating a new user, new site, etc.

The main domain would only provide a limited set of URL’s such as:
mydomain.com/about
mydomain.com/signup

The customer subdomains would all provide a completely different set of URL’s to the main domain. Their url’s shouldn’t work on the main domain and the main domains URL’s shoudn’t work on subdomains.

I’m battling to figure out how to do this. Initial I thought I could do a check in urls.py and conditionally load the urlpatterns based on whether it was the maindomain or not. Something like

If isMainDomain = True:
    #load url’s for main domain
    urlpatterns = […]
else:
    #load url’s for subdomains
    urlpatterns = […]

However, I realized that urlpatterns is loaded into memory when the server starts so this can’t work, as it would need to be evaluated for each request.

Does anyone have recommendations for how to implement this?

I could add a decorator on every view to check whether access is allowed based on if it’s the main domain or not, but I’d like to avoid doing that if possible.

I do want to keep the main domain within the Django project as where will be a couple of pages with functionality, it’s not just static pages.

Thanks in advance for any suggestions.

(Basil Jose) #2

Better to separate this into two and deal with nginx. or you may write custom middle where there you have to implement the logic for detecting the domain and then check requested url is in your allowed urls for that domain. or go for decorators but you have to use in every url as login_required

#3

Thanks for the suggestions Basil

(Vitor Freitas) #4

Usually for this kind of use case I use this app:

It allows you to use different url patters for subdomains. The deploy and setup is usually more complex. It will require you to setup a wildcard subdomain and handle it at application level.