7

我正在尝试使用django-subdomains应用程序在我的 Django 项目中配置子域。

这些是我采取的步骤:

  • 使用 pip 安装 django-subdomains。
  • 将“subdomains.middleware.SubdomainURLRoutingMiddleware”添加到我的 MIDDLEWARE,只是 b
  • 在我的设置中添加了 SITE_ID,并且还配置了 SUBDOMAIN_URLCONF

这是我的settings.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*********************************************'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # My application
    'myapp',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',

    # Sub-domains Middleware
    'subdomains.middleware.SubdomainURLRoutingMiddleware',

    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'


# Sub-domains

SITE_ID = 1

SUBDOMAIN_URLCONF = {
     None: 'mysite.urls',
    'www': 'mysite.urls',
    'blog': 'mysite.urls.blog',
    'shop': 'mysite.urls.shop'
}

我也有这个作为我的mysite.urls.py

from django.conf.urls import url,包括 from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    # My application URL
    url(r'^', include('myapp.urls', namespace='myapp'))
]

无论如何,我正在使用这个视图测试整个事情:

def index(request):
    return HttpResponse(request.subdomain)

当我运行命令python manage.py runserver时,我得到标题中的错误。我的控制台中的完整回溯可以在这里找到:

Pastebin 链接

Unhandled exception in thread started by <function wrapper at 0x1026ad2a8>
Traceback (most recent call last):
  File "/Users/user/mysite/myvenv/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/user/mysite/myvenv/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 142, in inner_run
    handler = self.get_handler(*args, **options)
  File "/Users/user/mysite/myvenv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
    handler = super(Command, self).get_handler(*args, **options)
  File "/Users/user/mysite/myvenv/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 64, in get_handler
    return get_internal_wsgi_application()
  File "/Users/user/mysite/myvenv/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 49, in get_internal_wsgi_application
    return import_string(app_path)
  File "/Users/user/mysite/myvenv/lib/python2.7/site-packages/django/utils/module_loading.py", line 20, in import_string
    module = import_module(module_path)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/user/mysite/mysite/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/Users/user/mysite/myvenv/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    return WSGIHandler()
  File "/Users/user/mysite/myvenv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 153, in __init__
    self.load_middleware()
  File "/Users/user/mysite/myvenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 82, in load_middleware
    mw_instance = middleware(handler)
TypeError: object() takes no parameters

我究竟做错了什么?我在安装过程中遗漏了一步吗?任何帮助表示赞赏!

4

2 回答 2

4

使用 MIDDLEWARE_CLASSES 插入 MIDDLEWARE 对我有用。

于 2017-05-17T10:54:39.560 回答
2

问题是,django 改变了中间件类的工作方式。他们期望 aMiddlewareMixin而不是object. 您可以做的是创建一个具有以下功能的自定义中间件SubdomainURLRoutingMiddleware

try:
    from django.utils.deprecation import MiddlewareMixin
except ImportError:
    MiddlewareMixin = object

class SubdomainMiddleware(MiddlewareMixin):

代替:

    class SubdomainMiddleware(object):

希望有帮助

于 2016-12-19T15:16:37.520 回答