TypeError:object()没有使用Django-subdomains

时间:2016-08-11 13:08:24

标签: python django django-subdomains

我正在尝试使用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

来自django.conf.urls导入网址,包含 来自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 Link

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

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

2 个答案:

答案 0 :(得分:3)

使用MIDDLEWARE_CLASSES MIDDLEWARE的instread对我有用。

答案 1 :(得分:2)

问题是,django改变了方式,中间件类工作。 他们希望MiddlewareMixin代替object。 您可以做的是创建一个具有SubdomainURLRoutingMiddleware功能的自定义中间件,并具有以下功能:

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

class SubdomainMiddleware(MiddlewareMixin):

而不是:

    class SubdomainMiddleware(object):

希望有所帮助