本文介绍了使用django-subdomains包的Django子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 django-subdomains软件包来创建子域.问题是,无论我如何配置SUBDOMAIN_URLCONFS,该站点始终会定向到默认情况下放置在ROOT_URLCONF中的任何内容.对于我做错了什么的任何见解将不胜感激!

I am using the django-subdomains package to create subdomains. The problem is that no matter how I configure the SUBDOMAIN_URLCONFS, the site always directs to whatever I have put in ROOT_URLCONF as a default. Any insight as to what I am doing incorrectly would be greatly appreciated!

添加了MIDDLEWARE_CLASSES

Added 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



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



myapp/views.py

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

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



myapptwo/urls.py



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



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!")

推荐答案

django-subdomains文档子域中间件应该位于CommonMiddleware之前

As noted in the django-subdomains docs the subdomain middleware should come before CommonMiddleware

因此您的设置应如下所示:

so your settings should look like this:

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',
)

这篇关于使用django-subdomains包的Django子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:29
查看更多