2017-07-30 72 views
1

我試圖讓django-tenant-schemas重新路由用戶,如果他們使用可選設置PUBLIC_SCHEMA_URLCONF訪問基本域。每當我參觀 基本URL我得到這樣的迴應:在django-tenant-schemas中使用PUBLIC_SCHEMA_URLCONF

enter image description here

我希望有人能告訴我什麼PUBLIC_SCHEMA_URLCONF值應根據我的項目結構,或者其他任何東西可能是錯的。當用戶嘗試訪問基本域時,我想使用public_website的網址。

我的項目目錄如下:

website 
├──approvals 
├──batches 
├──customauth 
├──email_portal 
├──exports 
├──file_downloads 
├──password_reset 
├──payroll 
├──payroll_codes 
├──reports 
├──scheduling 
├──shifts 
├──static 
├──templates 
├──website 
| ├──migrations 
| ├──static 
| ├──templates 
| └──settings 
|  ├──__init__.py 
|  ├──base.py 
|  ├──prod.py 
|  └──dev.py 
├──scheduling 
├──public_website 
| ├──__init__.py 
| └──urls.py 
└──manage.py 

而且我想PUBLIC_SCHEMA_URLCONF指在public_website的網址,這看起來像:

from django.conf.urls import include, url 

import website.views as website_views 
from django.contrib import admin 
from django.http import HttpResponse 

url_patterns = [ 
    url(r'^$', lambda request: HttpResponse('ok')), 
    url(r'^admin/login/', website_views.Login.as_view()), 
    url(r'^admin/', include(admin.site.urls)) # user authentication urls 
] 

以下是我的設置中的相關位:

DJANGO_APPS = (
    'jet', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'django_extensions', 
    'django.contrib.admin', 
    'django_user_agents', 
    'django_ses', 
    # 'admin_reorder' 
) 

MY_APPS_WITH_MODELS = (
    'customauth', 
    'payroll_codes', 
    'scheduling', 
    'payroll', 
    'shifts', 
    'email_portal', 
    'tutor_training_tracker' 
) 

MY_APPS_WITHOUT_MODELS = (
    'exports', 
    'reports', 
    'file_downloads', 
    'batches', 
    'approvals' 
) 

SHARED_APPS = (
    'tenant_schemas', 
    'website', 
    'public_website' 
) 
TENANT_APPS = DJANGO_APPS + MY_APPS_WITH_MODELS 
INSTALLED_APPS = list(OrderedDict.fromkeys(SHARED_APPS + DJANGO_APPS + MY_APPS_WITHOUT_MODELS + MY_APPS_WITH_MODELS)) 
ROOT_URLCONF = 'website.urls' 
PUBLIC_SCHEMA_URLCONF = 'public_website.urls' 
TENANT_MODEL = "website.Client" 
DEFAULT_FILE_STORAGE = "tenant_schemas.storage.TenantFileSystemStorage" 

任何幫助將不勝感激d。

解決方法:

(感謝Thomas寫我中間件)

添加自定義中間件類:

from django.conf import settings 
from django.db import connection 
from tenant_schemas.middleware import MIDDLEWARE_MIXIN 
from tenant_schemas.utils import remove_www, get_tenant_model, get_public_schema_name 


class CustomTenantMiddleware(MIDDLEWARE_MIXIN): 
    def get_tenant(self, model, hostname, request): 
     return model.objects.get(domain_url=hostname) 

    def hostname_from_request(self, request): 
     """ Extracts hostname from request. Used for custom requests filtering. 
      By default removes the request's port and common prefixes. 
     """ 
     return remove_www(request.get_host().split(':')[0]).lower() 

    def process_request(self, request): 
     # Connection needs first to be at the public schema, as this is where 
     # the tenant metadata is stored. 
     connection.set_schema_to_public() 

     hostname = self.hostname_from_request(request) 
     TenantModel = get_tenant_model() 

     try: 
      # get_tenant must be implemented by extending this class. 
      tenant = self.get_tenant(TenantModel, hostname, request) 
      assert isinstance(tenant, TenantModel) 
      request.tenant = tenant 
      connection.set_tenant(request.tenant) 

     except (TenantModel.DoesNotExist, AssertionError): 
      request.urlconf = settings.PUBLIC_SCHEMA_URLCONF 
      request.public_tenant = True 
      return 

     if hasattr(settings, 'PUBLIC_SCHEMA_URLCONF') and request.tenant.schema_name == get_public_schema_name(): 
      request.urlconf = settings.PUBLIC_SCHEMA_URLCONF 

,並設置引用它:

MIDDLEWARE_CLASSES = (
    # 'tenant_schemas.middleware.TenantMiddleware', 
    'website.middleware.CustomTenantMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
    'django.middleware.security.SecurityMiddleware', 
    'django_user_agents.middleware.UserAgentMiddleware', 
    'admin_reorder.middleware.ModelAdminReorder', 
    'website.middleware.CustomTenantMiddleware', 
    # 'djadmin.middleware.DJMiddleware', 
) 

回答

2

我維護Django-Tenants,這是django-tenant-schemas的一個分支。公共網址文件在域名與租戶不匹配時被調用。然而取決於你有這個中間件可能會導致這個不起作用。如果是這種情況,則爲租戶編寫自定義中間件。你可能也想編輯你的主機文件,因爲它很難通過編輯進行測試。你使用什麼中間件?

+0

我的主機文件設置爲允許tenant1.localhost和tenant2.localhost,並且這兩個網站工作正常。這只是本地主機,並不指引我在我想要的地方。我在中間件中列出了tenant_schemas.middleware.TenantMiddleware。 – DragonBobZ

+0

您是否更改了PUBLIC_SCHEMA_NAME? –

+0

我沒有改變它的默認值。我現在只是明確地將其設置爲「公開」而沒有改變。 – DragonBobZ