在django-tenant-schemas

时间:2017-07-30 19:48:20

标签: django

如果用户使用可选设置PUBLIC_SCHEMA_URLCONF访问基域,我试图让django-tenant-schemas重新路由用户。每当我访问 base url我得到了这个回复:

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"

非常感谢任何帮助。

解决方案:

(感谢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 个答案:

答案 0 :(得分:4)

我维护Django-Tenants,它是django-tenant-schemas的一个分支。当域与租户不匹配时,将调用公共URL文件。但是,根据中间件的不同,这会导致无法正常工作。如果是这种情况,请为租户编写自定义中间件。您可能还想编辑主机文件,因为很难通过编辑进行测试。你在用什么中间件?

答案 1 :(得分:0)

确保您已安装最新版本的 django-tenants 并且这些设置已在 settings.py 中设置:

SHOW_PUBLIC_IF_NO_TENANT_FOUND = True # 我想知道为什么文档中没有提到这个设置。

PUBLIC_SCHEMA_URLCONF = 'project_name.urls_public'