django-tenants / django-tenants

Django tenants using PostgreSQL Schemas
MIT License
1.52k stars 344 forks source link

Django Tenants + REST framework - tenant URL error 11011 #960

Open rzrwolf opened 1 year ago

rzrwolf commented 1 year ago

Hello!

I am trying to build some service app with django tenants and REST api

I am getting an error when trying to access API ..

For example if directly enter URLhttp://demo.localhost:9000/api/zakaz/2/ - i get correct API view

But if i am trying to access the same address from the Api call - i get the following error:

HTTPConnectionPool(host='demo.localhost', port=9000): Max retries exceeded with url: /api/zakaz/2/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000222A0947160>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

Basically app can not connect to API in code by using tenant domain as API URL as far as i understand..

How this can be solved ?

This is Settings file i use:

SHARED_APPS = [
    'django_tenants',  # mandatory
    'clients.apps.ClientsConfig',  # you must list the app where your tenant model resides in

    'django.contrib.contenttypes',

    'dal',
    'dal_select2',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'crispy_forms',
    'crispy_bootstrap4',
]

MIDDLEWARE = [
    'django_tenants.middleware.main.TenantMainMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

TENANT_APPS = (
    # your tenant-specific apps
    'rest_framework',
    'api.apps.ApiConfig',
    'orders.apps.OrdersConfig',
    'patients.apps.PatientsConfig',
)

INSTALLED_APPS = list(SHARED_APPS) + [app for app in TENANT_APPS if app not in SHARED_APPS]

ROOT_URLCONF = 'cabinetpartner.urls'
PUBLIC_SCHEMA_URLCONF = 'cabinetpartner.urls_public'

The tenant url.py is as follow:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('patients/', include('patients.urls')),
    path('', include('orders.urls')),
    path('api/', include('api.urls')),
]

API url.py


urlpatterns = [

        path('zakaz', StatusView.as_view()),
        path('zakaz/<int:pk>/', StatusView.as_view()),

        path('blank', BlankView.as_view()),

    ]
rzrwolf commented 1 year ago

Further to above - using HTTPIE from console - gives same error

http http://demo.localhost:9000/patients/patients

http: error: gaierror: [Errno 11001] getaddrinfo failed
http http://demo.localhost:9000/                 

http: error: gaierror: [Errno 11001] getaddrinfo failed
Couldn’t resolve the given hostname. Please check the URL and try again.

Does it mean that subdomain is not resolved as address?
What am i doing wrong?

rzrwolf commented 1 year ago

I spent almost 8 hours to fix it and now - i got the issue - for some reason browser resolves wildcard subdomains from address string, but Windows fails to do so for pycharm and python..

I have edited the HOSTS file adding the subdomains

127.0.0.1 demo.localhost
127.0.0.1 revital.localhost
127.0.0.1 localhost

And guess what? API and everything WORKS as expected...

How do i deal with tenant wildcard subdomains in Django and not just list them in HOSTS file?

Any ideas / working solutions?

Does this happen with Linux distros?