热门标签:
Q:

Django子域使用django-subdomains包

我正在使用django-subdomains包创建子域。 问题是,无论我如何配置SUBDOMAIN_URLCONFS,该网站始终指向我在ROOT_URLCONF中作为默认设置的任何内容。 任何关于我做的错误的见解都将不胜感激!

编辑:添加MIDDLEWARE_CLASSES


<强力>mysite/settings.py

...

MIDDLEWARE_CLASSES = (
    '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',
    'subdomains.middleware.SubdomainURLRoutingMiddleware',
)

...

ROOT_URLCONF = 'mysite.urls'

SUBDOMAIN_URLCONFS = {
    None: 'mysite.urls',
    'www': 'mysite.urls',
    'myapp': 'myapptwo.test',
}

...



<强力>mysite/urls.py

from django.conf.urls import patterns, include, url
from myapp import views
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^admin/', include(admin.site.urls)),
)



<强力>myapp/views.py

from django.shortcuts import render
from django.http import HttpResponse

def index(Request):
    return HttpResponse("Hello world.")



<强力>myapptwo/urls.py

from django.conf.urls import patterns, include, url
from myapptwo import views
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^admin/', include(admin.site.urls)),
)



<强力>myapptwo/views.py

from django.shortcuts import render
from django.http import HttpResponse

def index(Request):
    return HttpResponse("Hello world. This is the myapptwo subdomain!")
原网址
A:

django-subdomains文档中所述子域中间件应该放在CommonMiddleware之前

添加子域名。中间件。SubdomainURLRoutingMiddleware到你的DJANGO设置文件中的MIDDLEWARE_CLASSES。 如果你使用的是django。中间件。普通的。CommonMiddleware,子域中间件应该在CommonMiddleware之前。

所以你的设置应该是这样的:

MIDDLEWARE_CLASSES = (
    'subdomains.middleware.SubdomainURLRoutingMiddleware',
    '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',
)

相似问题