Show the main page in the django multi-tenant

Viewed 597

I am studing the djanto multi-tenant package (https://github.com/django-tenants/django-tenants).

When i try to access the main page (landing page) at http://127.0.0.1:8000/, I got the following message:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Raised by:  core.views.home
No tenant for hostname "127.0.0.1"

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to 

False, and Django will display a standard 404 page.

How do I get the landing page to show correctly?

URL FILE

from core.views import home

urlpatterns = [
     path('admin/', admin.site.urls),
     path('', home, name='home'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

THE VIEW OF LANDINGPAGE

from django.shortcuts import render

    def home(request):
        return render(request, 'core/index.html')

PARTIAL SETTINGS FILES

DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

SHARED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_tenants',
    'customer',
    'core', //landing page


]


TENANT_APPS = [
    # The following Django contrib apps must be in TENANT_APPS
    'django.contrib.contenttypes',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',

    # your tenant-specific apps
    'myclientapp',


]

INSTALLED_APPS = list(set(SHARED_APPS + TENANT_APPS))

Thank you very much!

1 Answers

The issue there is with the Django Tenants middleware. By default, django_tenants will show a Page 404 error if you go to a tenant that does not exist. You can change this behavior by over-modifying the middleware and having your own local copy or (the easiest solution) you can just provide a default case by adding SHOW_PUBLIC_IF_NO_TENANT_FOUND = True in your project's settings.py file. This will route to the public schema hence other url routes will work as they should.

Related